自动装配一个bean列表并单独识别它们

时间:2017-08-11 05:24:51

标签: java spring-mvc web spring-boot autowired

我可以在春季启动时自动挂载服务列表cond_wait,但我需要一种方法来使用他们给出的beans来选择我需要的服务。

name

是否可以从列表中单独获取 @Service("myService") public class DefaultService implements MyService { } @Service("myService2") public class DefaultService2 implements MyService { } @Autowire List<MyService> services; DefaultService2

5 个答案:

答案 0 :(得分:1)

添加@Qualifier注释:

@Autowired
@Qualifier("myService")
MyService myService;

答案 1 :(得分:1)

您可以使用:

MyClass

或使用资源注释。它应该工作。

@Autowired
@Qualifier("myService")
MyService myService;

答案 2 :(得分:1)

尝试在List中迭代您的bean,并使用反射api .getClass().getSimpleName()标识每个bean。这将返回您的类的String名称。

for(MyService service : services) {
    if(service.getClass().getSimpleName().equals("DefaultService")) {
        MyService defaultService = service; // This is DefaultService class
    }
    if(service.getClass().getSimpleName().equals("DefaultService2")) {
        MyService defaultService2 = service; // This is DefaultService2 class
    }
}

有了这个,你就会知道你正在使用的服务是什么。

答案 3 :(得分:1)

如果您想按名称获取服务,请按以下方式实施模式工厂:

您的服务实施:

public interface MyService {
    void sayHello();
}


public class DefaultService implements MyService {
    private static Logger log = LoggerFactory.getLogger(DefaultService.class);
    @Override
    public void sayHello() {
        log.info("Hello from DefaultService");
    }
}

public class DefaultService2 implements MyService {
    private static Logger log = LoggerFactory.getLogger(DefaultService2.class);
    @Override
    public void sayHello() {
        log.info("Hello from DefaultService2");
    }
}

工厂界面:

public interface MyServiceFactory {
    public MyService getMyServiceByName(String name);
}

工厂豆:

@Configuration
public class MyServiceFactoryBean {
    @Bean
    public FactoryBean serviceLocatorFactoryBean(){
        ServiceLocatorFactoryBean factoryBean = new ServiceLocatorFactoryBean();
        factoryBean.setServiceLocatorInterface(MyServiceFactory.class);
        return factoryBean;
    }

    @Bean("myService")
    @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public DefaultService defaultService(){
        return new DefaultService();
    }

    @Bean("myService2")
    @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public DefaultService2 defaultService2(){
        return new DefaultService2();
    }
}

用法:

   @Autowired
   MyServiceFactory myServiceFactory;

    @Override
    public void run(String... strings) throws Exception {
        myServiceFactory.getMyServiceByName("myService").sayHello();
        myServiceFactory.getMyServiceByName("myService2").sayHello();
    }

结果:

2017-08-11 11:32:31.126  INFO 12827 --- [  restartedMain] c.m.test.DefaultService    : Hello from DefaultService
2017-08-11 11:32:31.129  INFO 12827 --- [  restartedMain] c.m.test.DefaultService2   : Hello from DefaultService2

答案 4 :(得分:0)

我可以通过排序 autowire order 来单独取出豆子。现在不需要。但

@Order(value=1)
@Service
public class DefaultService implements MyService {
}

@Service
@Order(value=2)
public class DefaultService2 implements MyService {
}