自定义注释不适用于 spring Beans
我已经创建了我的新自定义注解@MyCustomAnnotation
I have created my new custom annotation @MyCustomAnnotation
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@Retention(RUNTIME)
public @interface MyCustomAnnotation{
}
我在组件和 bean 上应用了该注释.这是代码,
I applied that annotation on component and bean. Here is the code,
@MyCustomAnnotation
@Component
public class CoreBussinessLogicHandler implements GenericHandler<BussinessFile> {
//some bussiness logic
}
和
@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.所以这是代码,
Now i want all the beans annotated with @MyCustomAnnotation. So here is the code,
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,我应该更改哪些代码?
My question is why I am not getting Bean with name 'INCOMING_PROCESS_CHANNEL' as it has @MyCustomAnnotation ? If I want to get bean with name 'INCOMING_PROCESS_CHANNEL' what code changes should I do ?
发生这种情况是因为您的 bean 没有收到此注释,因为您已将其放置在bean 定义配置"上.所以它是可用的,但只能通过 BeanFactory 和 beanDefinitions.您可以将注释放在 bean 类上,也可以编写一个自定义方法来使用 bean 工厂进行搜索.
This happens because your bean does not receive this annotation as you have placed it on a "bean definition configuration". So it is available but only through BeanFactory and beanDefinitions. You can either put the annotation on your bean class or write a custom method that would do the searching using bean factory.
请参阅此处接受的答案一>.