为自定义注释创建一个spring BeanFactory

时间:2017-02-23 08:37:16

标签: spring spring-boot annotations

我有一个创建spring bean工厂的用例,该工厂扫描带有特定注释的注释类型,并根据注释参数创建spring bean。

这个bean工厂生成的spring bean是不同类型的,例如Class1,Class2。可以使用不同的限定符创建多个这些类型的实例。

最后,我应该可以在任何一个spring bean中使用@Autiwired,@ Inject等注释注入它们。

注意:我需要工厂以避免创建这么多标记组件。

例如 有一个注释@MyAnnotation接受参数“Class clazz”。

有两个使用此注释注释的类。

@MyAnnotation(clazz = Component1.class) FirstService {}
@MyAnnotation(clazz = Component2.class) FirstComponent {}
@MyAnnotation(clazz = Component1.class) SecondService {}

应该有一个组件使用这个注释过滤类,并使用自定义参数将它们分别注册为Component1,Component2和Component1的bean。

最后我想把它们注入任何类似

的春豆
@Autowired
Component1 component1Instance1;

@Autowired
Component2 component2;

@Autowired
Component1 component1Instance2;

我尝试使用ApplicationEvent,ApplicationContextAware,BeanPostProcessor等实现这个。但是,无法实现。

请提供任何暗示以使其成为可能。

0 个答案:

没有答案
相关问题