Java 为什么在junit测试期间多次初始化bean?

Java 为什么在junit测试期间多次初始化bean?,java,spring,junit,Java,Spring,Junit,你能帮我理解为什么会发生以下情况吗? 我有以下bean: import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class TestBean implements BeanPostProcessor { public void init() { System.out.println

你能帮我理解为什么会发生以下情况吗? 我有以下bean:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class TestBean implements BeanPostProcessor {
    public void init() {
        System.out.println("Initialized!");
    }

    public void destroy() {
        System.out.println("Destroyed!");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization");
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization");
        return null;
    }
}
使用以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean class="TestBean" init-method="init" destroy-method="destroy"/>
</beans>
现在,让我惊讶的是,如果我运行单元测试,postProcessBeforeInitialization和postProcessAfterInitialization会分别打印5次。原因是什么?在bean初始化期间,它们不应该只打印一次吗?
提前谢谢你

我认为文档和方法签名应该有助于您理解这个问题

允许自定义修改新bean的工厂钩子 实例,例如,检查标记接口或使用 代理


我建议您也打印出bean名称。这将向您展示像EventListnerFactorys这样的bean以及其他以编程方式注入bean的工厂方法。因此,在注入之前,会为这些bean中的每一个调用此方法,这样您就有机会在需要时代理或包装bean。

我认为文档和方法签名应该有助于您理解问题

允许自定义修改新bean的工厂钩子 实例,例如,检查标记接口或使用 代理


我建议您也打印出bean名称。这将向您展示像EventListnerFactorys这样的bean以及其他以编程方式注入bean的工厂方法。因此,在注入之前,会对这些bean中的每一个调用此方法,以便您有机会在需要时代理或包装您的bean。

回顾一下我的评论:

对TestBean后处理器postProcessBeforeInitialization方法稍作更改:

可能的最小Spring依赖关系:org.springframework:Spring context:5.0.2.RELEASE

代码的输出为:

postProcessBeforeInitialization org.springframework.context.event.internalEventListenerProcessor
postProcessBeforeInitialization org.springframework.context.event.internalEventListenerFactory
postProcessBeforeInitialization TestBeanTest
结论:后处理器只对bean进行一次后处理,其余的都是Spring自己的bean。事实上,你得到5,我只得到3,这是我们使用的不同依赖项集


另外,请注意,TestBean甚至不被认为是打印的,因为它不是一个bean,而是一个bean后处理器。

回顾一下我的评论:

对TestBean后处理器postProcessBeforeInitialization方法稍作更改:

可能的最小Spring依赖关系:org.springframework:Spring context:5.0.2.RELEASE

代码的输出为:

postProcessBeforeInitialization org.springframework.context.event.internalEventListenerProcessor
postProcessBeforeInitialization org.springframework.context.event.internalEventListenerFactory
postProcessBeforeInitialization TestBeanTest
结论:后处理器只对bean进行一次后处理,其余的都是Spring自己的bean。事实上,你得到5,我只得到3,这是我们使用的不同依赖项集


另外,请注意,TestBean甚至不被认为是打印的,因为它不是一个bean,而是一个bean后处理器。

testCollectionBean方法实际上做什么?您的测试类中有多少@Test方法?@Arnaud Nothing。它故意留为空。@StefanBirkner我放在那里的这个测试类是完整的-只有一个,空的测试有由Spring本身配置的bean。尝试:System.out.printlnpostProcessBeforeInitialization+beanName。其中只有一个应该是您的testCollectionBean方法实际上做什么?您的测试类中有多少@Test方法?@Arnaud什么都没有。它故意留为空。@StefanBirkner我放在那里的这个测试类是完整的-只有一个,空的测试有由Spring本身配置的bean。尝试:System.out.printlnpostProcessBeforeInitialization+beanName。其中只有一个应该是你的
postProcessBeforeInitialization org.springframework.context.event.internalEventListenerProcessor
postProcessBeforeInitialization org.springframework.context.event.internalEventListenerFactory
postProcessBeforeInitialization TestBeanTest