简体   繁体   English

如何自检Spring bean的作用域?

[英]How to self-check the Spring bean scope?

My project provides a number (a nuuuuumbeeeeer) of base classes that developers may extend to their comfort and have to declare as Spring beans into XML configuration.我的项目提供了许多(一个 nuuuuumbeeeeer)基类,开发人员可以根据需要扩展这些基类,并且必须在 XML 配置中声明为 Spring bean。

I am already aware that annotation config is inherited from the base class, but we mainly use XML我已经知道annotation config是从基类继承的,但是我们主要使用XML

@SpringAnnotation
public abstract class Parent{}

//Will apply any @SpringAnnotation
public class Child{}

What I want to do is a self check in the base class.我想做的是在基类中进行自检。 If a developer forgets to set the scope of a bean to the documented value (say, "request", for the current example), I would like the inherited bean to throw an exception reminding the developer to change the scope.如果开发人员忘记将 bean 的范围设置为文档值(例如,对于当前示例,“请求”),我希望继承的 bean 抛出异常,提醒开发人员更改范围。

Eg例如

public void afterPropertiesSet()
{
    if (!thisBean.getScope().equals("request"))
        throw new BeansException("Sorry but this bean works only when it is a request bean");
}

I have seen that Spring does not define any kind of BeanDefinitionAware which might inject the bean's own definition (along with scope and a lot of stuff) into the bean itself.我已经看到 Spring 没有定义任何类型的BeanDefinitionAware ,它可能会将 bean 自己的定义(以及作用域和很多东西)注入 bean 本身。

Any idea?任何的想法? I may come with a self-made solution soon.我可能很快就会提出一个自制的解决方案。

You can define a bean implementing the BeanFactoryPostProcessor interface.您可以定义一个实现BeanFactoryPostProcessor接口的 bean。

As reported in the BeanFactoryPostProcessor Spring doc , a bean implementing BeanFactoryPostProcessor is called when all bean definitions will have been loaded, but no beans will have been instantiated yet.正如BeanFactoryPostProcessor Spring doc 中所报告的那样,当所有 bean 定义都已加载但尚未实例化任何 bean 时,将调用实现BeanFactoryPostProcessor bean。 This way you will be able to read all the beans definitions and check if the beans are properly configured.通过这种方式,您将能够读取所有 bean 定义并检查 bean 是否已正确配置。

This is an example:这是一个例子:

public class CustomBeanFactory implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        for (String beanName : beanFactory.getBeanDefinitionNames()) {

            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);

            // Analyze the bean definition

        }
    }
}

Last but not least, remember that a BeanFactoryPostProcessor may interact with and modify bean definitions, but never bean instances.最后但并非最不重要的一点,请记住BeanFactoryPostProcessor可以与 bean 定义交互并修改 bean 定义,但绝不能与 bean 实例交互。 Doing so may cause premature bean instantiation, violating the container and causing unintended side-effects.这样做可能会导致 bean 过早实例化、违反容器并导致意外的副作用。 If bean instance interaction is required, consider implementing BeanPostProcessor instead.如果需要 bean 实例交互,请考虑实现BeanPostProcessor

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM