Could we autowire an interface without any implementation in Spring?

北慕城南 提交于 2019-12-11 05:49:32

问题


We have to share our code base with a partner for developing, but we don't want to reveal the implementation of some services.

Say we have an interface FooService and its implementation FooServiceImpl.

public interface FooService
{
    void doSomething();
}

@Service
public class FooServiceImpl implements FooService
{
    @Override
    public String doSomething();
    {
        ...
    }
}

Many other classes autowire this service in and call doSomething(). For example:

@Service
public class BarServiceImpl implements BarService
{
    @Autowired
    private FooService  fooService;

    @Override
    public String validate();
    {
        fooService.doSomething();
        ...
    }
}

If I just delete FooServiceImpl, of course a NoSuchBeanException will be thrown while starting. If I use @Autowired(required = false) everywhere that FooService is autowired, a NullPointerException will be thrown at runtime whenever doSomething() gets called.

Besides the removal of each method body in FooServiceImpl manually, is there any other way to work this around?

Any help is appreciated.


回答1:


I agree with chrylis; your use case sounds like you should ship a default implementation. I would also configure your code to allow the users of your library to provide their own implementation. Spring lets you do this easily with their @Conditional... annotations. Specifically, I think you should use @ConditionalOnMissingBean.

Default implementation

@Component
public class DefaultFooServiceImpl implements FooService {
  @Override
  public void doSomething() {
    LOG.info("Using default implementation of doSomething(); nothing will happen");
  }
}

Sample configuration

@Configuration
public class ServiceConfig {
  @ConditionalOnMissingBean(FooService.class)
  @Bean
  DefaultFooServiceImpl defaultFooServiceImpl() {
    return new DefaultFooServiceImpl();
  }
}

When your partner uses your library, they get the default implementation, as well as the option to provide a better implementation. When you use your library, you can create a ProprietaryFooServiceImpl bean and it will get injected instead.




回答2:


First of all, as @AdrianShum and @chrylis commented, you need an implementation of FooService for BarServiceImpl.validate() to work. So it's not clear what you mean by "don't want to reveal the implementation of some services".

  • NoSuchBeanException - As you are autowiring FooService in BarServiceImpl, scanning will be done to find a bean of FooService type. As you don't have any implementation of FooService, this error occurs.
  • NullPointerException - As required=false, above problem is neglected during initialization. But the private FooService fooService will have a NULL value obviously. So exception occurs when you call fooService.doSomething().
  • Now you may think, may be you can put @Component (or similar thing) on your interface. From the Spring docs, "This annotation is for the implementation classes to be autodetected through classpath scanning". Then there will be NoSuchBeanException, as you have no implementation classes.

So, you can't autowire an interface without any implementation in Spring.




回答3:


Could we autowire an interface without any implementation in Spring?

No.

You are trying to create instance for an Interface which is not possible in Java.



来源:https://stackoverflow.com/questions/41498104/could-we-autowire-an-interface-without-any-implementation-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!