Spring Java Config - 使用Interface公开bean

时间:2015-11-24 21:54:17

标签: spring dependency-injection spring-java-config

我有一个班级,让我们说' DefaultService'它实现了两个接口:' Service1'和' Service2'。 Spring java配置如下所示:

@Bean
Service1 defaultService() {
    return new DefaultService();
}

现在,我有另一个需要' Service2'。

的bean
public class Foo implements AnotherInterface {
   @Autowired
   private Service2 service2;
}

并且这个bean也是通过Java配置配置的:

@Bean
AnotherInterface anotherInterface(){
   return new Foo();
}

Spring不喜欢这种配置。我认为这是有道理的,因为' DefaultService'暴露为“服务1”,而不是“服务2”和“服务2”。 (Foo要求)。

No qualifying bean of type [...Service2] found for dependency: expected at least 1 bean which qualifies ...

我当然可以将DefaultService公开为Service2。但是,如果有另一个bean需要Service1呢?对于这种情况,Spring建议是什么?我发现的另一个(奇怪的)问题是以下配置有效:

@Bean
Service2 defaultService(){ // exposing the bean as Service2, to fix dependency on Foo
   return new DefaultService(); 
}

@Bean
AnotherDependant anotherDependant(Service1 service1){
   return new AnotherDependant(service1);
}

Spring如何将Service1连接到' AnotherDependant'的配置声明? (对于@Autowired,我对第一个场景感到高兴)? 我使用Spring 3.2.2.RELEASE,虽然我怀疑这个版本真的很重要..

我的最佳解决方法是:

@Bean
DefaultService defaultService(){
return new DefaultService();
}

@Bean
Service1 service1(){
return defaultService();
}

@Bean
Service2 service2(){
return defaultService();
}

但这太丑了......

=============================================== ======================== 回复@Oskar。 基本上@Oskar所建议的与在xml中声明两个相同...即在spring容器中创建两个相同类的实例。

public interface Driveable {}
public interface Vehicle {}
public class Car implements Vehicle, Driveable{}


@Configuration
public class Config {
    @Bean
    public Vehicle vehicle() {
        return new Car();
    }
    @Bean
    public Driveable driveable() {
        return new Car();
    }
}

public class Main {
    public static void main(String[] args) {
        final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

        final Driveable driveable = context.getBean("driveable", Driveable.class);
        final Vehicle vehicle = context.getBean("vehicle", Vehicle.class);
        System.out.println(driveable == vehicle);
    }
}

1 个答案:

答案 0 :(得分:1)

public interface Driveable {}
public interface Vehicle {}

@Component
public class Car implements Vehicle, Driveable{}


@Configuration
public class Config {

    @Autowired
    private Car car;

    @Bean
    public Vehicle vehicle() {
        return car;
    }

    @Bean
    public Driveable driveable() {
        return car;
    }
}

public class Application {

    public static void main(String[] args) throws Exception {
        final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, Car.class);
        final Driveable driveable = context.getBean("driveable", Driveable.class);
        final Vehicle vehicle = context.getBean("vehicle", Vehicle.class);
        System.out.println(driveable == vehicle);
    }

}

以问题中的汽车为例,

  1. 将Car定义为@Component。
  2. 在Config中声明一个自动装配的字段Car car
  3. 使用@Bean带注释的方法返回自动装配字段。
  4. 如果我们使用@ComponentScan,则会自动选择汽车组件。另外,如果我们自己创建上下文,我们可以在AnnotationConfigApplicationContext构造函数中传递Car类(如代码所示)。