列出 Spring 个 Bean
Make List of Spring Bean
我有接口 EventService
和 class @Component Event
实现它。 Class @Component BerlinEvent
扩展 @Component Event
并实现 EventService
.
关于配置 class 我有这个:
@Configuration
public class Configuration {
//Country name
@Bean
@ConditionalOnProperty(name = "country", havingValue = "UK")
public Event defaultService(){return new Event();}
@Bean
@ConditionalOnProperty(name = "country", havingValue = "germany", matchIfMissing = true)
public Event germanyEventService(){return new BerlinEvent();}
}
主要是我做豆子:
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(EventscraperApplication.class, args);
EventsManagerService eventsManager = context.getBean(EventsManager.class);
eventsManager.run(context.getBean(Event.class));
}
现在 class EventsManagerService
我需要用 BerlinEvent
或 Event
对象创建一个列表,具体取决于创建的 bean 和每个具有不同值的对象但是我不知道该怎么做
在你的 bean class 中,你可以做
@Service
public class EventsManagerService {
@Autowired
private ApplicationContext applicationContext;
private Map<String, Event> beans;
@PostConstruct
public void setMocks() {
beans = applicationContext.getBeansOfType(Event.class);
}
}
这将为您提供实现事件的所有 bean class。
Spring 可以像这样将实现相同接口的所有 bean 自动装配到列表中
@Autowired
private List<Event> events;
默认情况下,只要有零个候选 bean 可用,自动装配就会失败;默认行为是将带注释的方法、构造函数和字段视为指示所需的依赖项。可以如下所示更改此行为。为避免这种情况,您需要像这样将 addtitonal 参数传递给注释:
@Autowired(required = false)
private List<Event> events;
这是 link 到 Spring 的文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-autowired-annotation
我有接口 EventService
和 class @Component Event
实现它。 Class @Component BerlinEvent
扩展 @Component Event
并实现 EventService
.
关于配置 class 我有这个:
@Configuration
public class Configuration {
//Country name
@Bean
@ConditionalOnProperty(name = "country", havingValue = "UK")
public Event defaultService(){return new Event();}
@Bean
@ConditionalOnProperty(name = "country", havingValue = "germany", matchIfMissing = true)
public Event germanyEventService(){return new BerlinEvent();}
}
主要是我做豆子:
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(EventscraperApplication.class, args);
EventsManagerService eventsManager = context.getBean(EventsManager.class);
eventsManager.run(context.getBean(Event.class));
}
现在 class EventsManagerService
我需要用 BerlinEvent
或 Event
对象创建一个列表,具体取决于创建的 bean 和每个具有不同值的对象但是我不知道该怎么做
在你的 bean class 中,你可以做
@Service
public class EventsManagerService {
@Autowired
private ApplicationContext applicationContext;
private Map<String, Event> beans;
@PostConstruct
public void setMocks() {
beans = applicationContext.getBeansOfType(Event.class);
}
}
这将为您提供实现事件的所有 bean class。
Spring 可以像这样将实现相同接口的所有 bean 自动装配到列表中
@Autowired
private List<Event> events;
默认情况下,只要有零个候选 bean 可用,自动装配就会失败;默认行为是将带注释的方法、构造函数和字段视为指示所需的依赖项。可以如下所示更改此行为。为避免这种情况,您需要像这样将 addtitonal 参数传递给注释:
@Autowired(required = false)
private List<Event> events;
这是 link 到 Spring 的文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-autowired-annotation