我创建了新的自定义注释@MyCustomAnnotation
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@Retention(RUNTIME)
public @interface MyCustomAnnotation{
}
我将该注释应用于组件和 bean。这是代码,
@MyCustomAnnotation
@Component
public class CoreBussinessLogicHandler implements GenericHandler<BussinessFile> {
//some bussiness logic
}
And
@Configuration
public class BussinessConfig {
@Autowired
private CoreIntegrationComponent coreIntegrationComponent;
@MyCustomAnnotation
@Bean(name = INCOMING_PROCESS_CHANNEL)
public MessageChannel incomingProcessChannel() {
return coreIntegrationComponent.amqpChannel(INCOMING_PROCESS_CHANNEL);
}
//some other configurations
}
现在我想要所有用@MyCustomAnnotation 注释的bean。所以这是代码,
import org.springframework.context.ApplicationContext;
@Configuration
public class ChannelConfig {
@Autowired
private ApplicationContext applicationContext;
public List<MessageChannel> getRecipientChannel(CoreIntegrationComponent coreIntegrationComponent) {
String[] beanNamesForAnnotation = applicationContext.getBeanNamesForAnnotation(MyCustomAnnotation.class);
//Here in output I am getting bean name for CoreBussinessLogicHandler Only.
}
}
我的问题是为什么我没有得到名称为“INCOMING_PROCESS_CHANNEL”的 Bean,因为它有 @MyCustomAnnotation ?如果我想获得名称为“INCOMING_PROCESS_CHANNEL”的 bean,我应该做什么代码更改?
发生这种情况是因为您的 bean 没有收到此注释,因为您已将其放置在“bean 定义配置”上。所以它是可用的,但只能通过 BeanFactory 和 beanDefinitions 来使用。您可以将注释放在 bean 类上,也可以编写一个自定义方法来使用 bean 工厂进行搜索。
See the 此处接受的答案 https://stackoverflow.com/questions/14236424/how-can-i-find-all-beans-with-the-custom-annotation-foo.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)