【问题标题】:Spring BeanPostProcessor invoked 3 timesSpring BeanPostProcessor 调用 3 次
【发布时间】:2017-02-11 10:56:52
【问题描述】:

我正在做一个 BeanPostProcessor 实现。我的 Bean PostProcessor 执行了 3 次。我在 init 方法之前额外调用了 2 次后处理器。我无法找到它发生的原因。请帮帮我。

我的代码和配置文件如下

BeanPostProcesssor 实现

public class BeanPP implements BeanPostProcessor{

    @Override
    public Object postProcessBeforeInitialization(Object o, String string) throws BeansException {
        System.out.println("---before initialization ------");
        return o;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String string) throws BeansException {
        System.out.println("----After initializaion ---- ");
        return o;
        //To change body of generated methods, choose Tools | Templates.
    }        
}

客户 Bean

public class Customer {        
  private String name;

  public String getName() {
      System.out.println("..getName.....");
      return name;
  }

  public void setName(String name) {
      System.out.println("..setName.....");
      this.name = name;
  }

  @PostConstruct
  public void init(){
      System.out.println("....Bean is going though init method");
  }

  @PreDestroy
  public void destory(){
      System.out.println("....Bean is going to destroy.......");
  }
}

配置类

@Configuration
public class AppConfig {
    @Bean(name="customer")
    public Customer getCustomer(){
        return new Customer();
    }
    @Bean
    public BeanPP getPP(){
        return new BeanPP();
    }

}

主类

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);
        Customer customer = (Customer)appContext.getBean("customer");
        customer.setName("test user ");
        System.out.println(".Name is .."+customer.getName());
    }
}

输出

---初始化前------ ----初始化后---- ---初始化前 ------ ----初始化后---- ---初始化前 ------ ....Bean 正在使用 init 方法 ----初始化后---- ..设置名称..... ..获取名称..... .Name 是 ..test 用户

【问题讨论】:

  • 你有 3 个 bean,它会为所有它们调用。你有AppConfigCustomerBeanPP,这三个都是豆子。

标签: java spring


【解决方案1】:

普通 bean 工厂允许以编程方式注册 后处理器,应用于通过该工厂创建的所有 bean

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/config/BeanPostProcessor.html

您已经定义了 3 个 bean,AppConfig、customer 和 PP。 bean 后处理器将在每个 bean 创建后执行。如果你有 3 个 bean,它将被执行 3 次。

@Configuration
public class AppConfig {
    @Bean(name="customer")
    public Customer getCustomer(){
        return new Customer();
    }
    @Bean
    public BeanPP getPP(){
        return new BeanPP();
    }
}

@Configuration 还定义了一个 bean,因为它继承自 @Component。

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

【讨论】:

  • 你好 Reos,谢谢你的意思。感谢您格式化我的代码。这是我的第一个代码。很抱歉给您带来不便。
【解决方案2】:

你能不能把postProcessBeforeInitialization也打印出Object o类来看看哪个类调用了BeanPostProcessor(它可能不是Customer类,而是Spring启动的其他一些对象,甚至是BeanPostProcessor本身)。

@Override
public Object postProcessBeforeInitialization(Object o, String string) throws BeansException {
    System.out.println("---before initialization ------ " + o..getClass().getName());
    return o;
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

【讨论】:

    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 2014-07-17
    • 2012-06-22
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2019-02-12
    • 2017-01-02
    相关资源
    最近更新 更多