【问题标题】:PostConstruct getting called before BeanPostProcessorPostConstruct 在 BeanPostProcessor 之前被调用
【发布时间】:2016-04-06 00:59:23
【问题描述】:

我目前是春天的新手。我试图遵循 PostConstruct 和 BeanPostProcessor 的调用顺序。

根据我了解到的以下是顺序:-

  1. BPP -> postProcessBeforeInitialization
  2. 后构造
  3. BPP -> postProcessAfterInitialization

但是我看到以下顺序被遵循:-

  1. 后期构造
  2. BPP -> postProcessBeforeInitialization
  3. 后构造
  4. BPP -> postProcessAfterInitialization

SpringConfig 文件 foo.xml 移除了 beans 标签 上下文:组件扫描 base-package="springtest"

@Component
public class MySpring implements ApplicationContextAware,BeanPostProcessor{

public static int temp =0;

public MySpring(){
    System.out.println("Initializing MySpring Constructor");
}

@PostConstruct
public void temp(){
    System.out.println("PostConsturct" + this.getClass());
    temp++;
}

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("Before BPP " + bean.getClass());

    return this;
}

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("After BPP " + bean.getClass());

    return this;
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("Initializing ApplicationContext");

}}

回应

  1. 初始化 MySpring 构造函数
  2. 初始化 ApplicationContext
  3. PostConsturctclass springtest.MySpring
  4. 属性设置类 springtest.MySpring 之后
  5. 在 BPP 类 org.springframework.context.event.EventListenerMethodProcessor 之前
  6. PostConsturctclass springtest.MySpring
  7. 属性设置类 springtest.MySpring 之后
  8. 在 BPP 类 springtest.MySpring 之后
  9. 在 BPP 类 org.springframework.context.event.DefaultEventListenerFactory 之前
  10. PostConsturctclass springtest.MySpring
  11. 属性设置类 springtest.MySpring 之后
  12. 在 BPP 类 springtest.MySpring 之后

MySpring.temp 值为 3 表示 PostContruct 被调用了 3 次。

有人可以在上面帮助我吗...

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    它被调用了 3 次,因为你用 MySpring bean 替换了每个 bean。

    你的方法

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Before BPP " + bean.getClass());
    
        return this;
    }
    

    正在返回this,实际上是说您当前正在后处理的bean 对象应该替换为MySpring 对象。您可以通过尝试从您的 ApplicationContext 获取任何其他 bean 来验证这一点。

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigurationBean.class);
    ctx.getBean(ConfigurationBean.class);
    

    这将失败并显示NoSuchBeanDefinitionException

    您的后处理方法应该返回其bean 参数的值。

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Before BPP " + bean.getClass());
    
        return bean;
    }
    

    @PostConstruct 调用使用自己的BeanPostProcessorCommonAnnotationBeanPostProcessor 实现。已注册的BeanPostProcessor 实例按顺序使用。

    当您的ApplicationContext 初始化您的MySpring 实例时,CommonAnnotationBeanPostProcessor 已经初始化并因此处理您的bean。 MySpring 完全初始化后,Spring 检测到它是一个BeanPostProcessor 并注册它。它在CommonAnnotationBeanPostProcessor 之前注册它(BeanPostProcessor 实例有优先级设置)。

    【讨论】:

    • 在我正在阅读的一些链接中,它说每个 bean 的 postProcessBeforeInitialization 和 postProcessAfterInitialization 将在移动到下一个 bean 之前被调用。但是,当我运行相同的程序时,我看到以下内容:-org.springframework.context.event.EventListenerMethodProcessor --> postProcessBeforeInitialization(对所有 bean 调用)、postProcessAfterInitialization(对所有 bean 调用)、org.springframework.context.event。 DefaultEventListenerFactory postProcessBeforeInitialization(所有bean调用),postProcessAfterInitialization(所有bean调用),
    • @confusedmind 我不明白您在评论输出中的困惑是什么。请编辑您的问题并将其添加到那里。是的,每个 bean 都会调用一个 BeanPostProcessor(一旦它被初始化)。
    • 你能帮我理解什么是 org.springframework.context.event.DefaultEventListenerFactory , org.springframework.context.event.EventListenerMethodProcessor 类吗?我也无法理解为什么这个 PostBean 处理器用于适当的业务示例?为什么 PostConstruct 不够?我目前指的是链接stackoverflow.com/questions/29743320/…,它似乎与我看到的响应不匹配。你能看看并告诉我链接是否有效(Bean Initialization Steps)框?
    • @confusedmind 查看javadoc for those classes,它们看起来像是事件API 中的一些类。您必须在 ApplicationContext 中将它们注册为 bean。它们与您的问题无关。它们可以被任何其他 bean 替换。你会看到同样的行为。
    • 嗨,我没有注册他们。我的 applicationContext.xml 中只有一个标签
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2012-02-04
    • 2015-07-19
    • 2021-06-06
    相关资源
    最近更新 更多