简体   繁体   English

使用自定义注释注释的bean的自定义初始化

[英]Custom initializing of beans annotated with a custom annotation

Say for example, I have some beans annotated with @Foo , and I want to keep track of these because I want to control what happens when they are initialized, is there a way to register a custom spring beanfactory that will allow me to do this? 比如说,我有一些用@Foo注释的@Foo ,我想跟踪它们,因为我想控制它们初始化时发生的事情,有没有办法注册自定义的spring beanfactory,这将允许我执行此操作?

What if I had another annotation @Bar which also needs this special initialization? 如果我还有另一个注释@Bar也需要这种特殊的初始化怎么办?

My initial thought was to inform the user to annotated each bean with @Lazy annotation, then using a bean factory post processor, I will change some properties of the bean definition. 我最初的想法是通知用户使用@Lazy注释对每个bean进行注释,然后使用bean工厂的后处理器,我将更改bean定义的某些属性。

The solution is to implement the BeanFactoryPostProcessor interface. 解决方案是实现BeanFactoryPostProcessor接口。 This gives us access to the BeanDefinition before any of the beans are instantiated, therefore allowing us to change things like the scope, or make the bean lazy initialized or even change the constructor arguments of the bean! 这使我们可以实例化任何Bean 之前访问BeanDefinition ,因此允许我们更改诸如范围之类的内容,或者使Bean延迟初始化,甚至更改Bean的构造函数参数!

If your spring application is manually started ie by creating a SpringApplicationBuilder , then you can even pass an instance of this class to the constructor of the builder and it will be used once the application is started. 如果您的spring应用程序是手动启动的,即通过创建SpringApplicationBuilder ,那么您甚至可以将此类的实例传递给构建器的构造函数,并且在应用程序启动后将使用该实例。

@Component
public class FooBarBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
        /*
        String[] fooBeans = beanFactory.getBeanNamesForAnnotation(Foo.class);
        BeanDefinition bean = beanFactory.getBeanDefinition(...);

        /* do your processing here ... */
    }
}

ps @Component annotation is required for this to work 必须使用ps @Component批注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到具有自定义注释的Bean - Beans with custom annotation not found 如何仅序列化使用自定义注释注释的属性 - how to only serialize properties annotated with a custom annotation 如何使用带有参数的自定义注释查找CDI bean? - How to find CDI beans with a custom annotation with parameters? 上下文组件扫描未找到我的自定义带注释的Bean - Context Component Scan not finding my custom annotated beans 查找所有具有自定义注释的bean,并从此bean创建解析器 - Find all beans with custom annotation and create resolver from this beans 如何排除对使用特定注释注释但未使用其他注释注释的bean的扫描? - How to exclude scanning of beans that annotated with specific annotation and not annotated with another annotation? 如何构建用我的自定义注释注释的类列表? - How to build a list of classes annotated with my custom annotation? 如何使用自定义注释来验证带有Spring @Value字段的注释? - How to validate annotated with Spring @Value field using custom annotation? Spring 自定义@Enable 注释使用@ComponentScan 元注释 - Spring custom @Enable annotation meta-annotated with @ComponentScan 显示使用自定义注释注释的方法/构造函数的警告? - Show warning for usage of methods/constructor annotated with custom annotation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM