繁体   English   中英

Spring Controller中的init方法(注释版)

[英]Init method in Spring Controller (annotation version)

我正在将 controller 转换为较新的注释版本。 在旧版本中,我曾经使用以下方法在 springmvc-servlet.xml 中指定 init 方法:

<beans>
    <bean id="myBean" class="..." init-method="init"/>
</beans>

如何使用注释版本指定 init 方法?

您可以使用

@PostConstruct
public void init() {
   // ...
}

或者,您可以让您的类实现InitializingBean接口,以提供一个回调函数( afterPropertiesSet() ),ApplicationContext将在构造bean时调用该函数。

有几种方法可以拦截Spring中的初始化过程。 如果你必须初始化所有bean并自动装配/注入它们,我至少有两种方法可以确保这一点。 我只测试了第二个,但我相信两个都是一样的。

如果你正在使用@Bean,你可以通过initMethod引用,就像这样。

@Configuration
public class BeanConfiguration {

  @Bean(initMethod="init")
  public BeanA beanA() {
    return new BeanA();
  }
}

public class BeanA {

  // method to be initialized after context is ready
  public void init() {
  }

} 

如果您正在使用@Component,可以像这样使用@EventListener进行注释。

@Component
public class BeanB {

  @EventListener
  public void onApplicationEvent(ContextRefreshedEvent event) {
  }
}

在我的情况下,我有一个遗留系统,我现在正在使用IoC / DI,其中Spring Boot是选择框架。 旧系统为表带来了许多循环依赖,因此我必须使用setter-dependency。 这给了我一些头痛,因为我无法信任@PostConstruct,因为setter的自动装配/注射还没有完成。 顺序是构造函数,@ PostConstruct然后是自动装配的setter。 我用@EventListener注释解决了这个问题,这个注释将最后运行并且在所有bean的“相同”时间运行。 该示例显示了InitializingBean的实现。

我有两个相互依赖的类(@Component)。 出于本示例的目的,这些类看起来相同,只显示其中一个。

@Component
public class BeanA implements InitializingBean {
  private BeanB beanB;

  public BeanA() {
    log.debug("Created...");
  }

  @PostConstruct
  private void postConstruct() {
    log.debug("@PostConstruct");
  }

  @Autowired
  public void setBeanB(BeanB beanB) {
    log.debug("@Autowired beanB");
    this.beanB = beanB;
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    log.debug("afterPropertiesSet()");
  }

  @EventListener
  public void onApplicationEvent(ContextRefreshedEvent event) {
    log.debug("@EventListener");
  } 
}

这是日志输出,显示容器启动时的调用顺序。

2018-11-30 18:29:30.504 DEBUG 3624 --- [           main] com.example.demo.BeanA                   : Created...
2018-11-30 18:29:30.509 DEBUG 3624 --- [           main] com.example.demo.BeanB                   : Created...
2018-11-30 18:29:30.517 DEBUG 3624 --- [           main] com.example.demo.BeanB                   : @Autowired beanA
2018-11-30 18:29:30.518 DEBUG 3624 --- [           main] com.example.demo.BeanB                   : @PostConstruct
2018-11-30 18:29:30.518 DEBUG 3624 --- [           main] com.example.demo.BeanB                   : afterPropertiesSet()
2018-11-30 18:29:30.518 DEBUG 3624 --- [           main] com.example.demo.BeanA                   : @Autowired beanB
2018-11-30 18:29:30.518 DEBUG 3624 --- [           main] com.example.demo.BeanA                   : @PostConstruct
2018-11-30 18:29:30.518 DEBUG 3624 --- [           main] com.example.demo.BeanA                   : afterPropertiesSet()
2018-11-30 18:29:30.607 DEBUG 3624 --- [           main] com.example.demo.BeanA                   : @EventListener
2018-11-30 18:29:30.607 DEBUG 3624 --- [           main] com.example.demo.BeanB                   : @EventListener

如您所见,@ EventListener在一切准备就绪并配置完成后运行。

@PostConstruct、implement InitializingBean、指定init-method他们有调用顺序。 所以你不能用它们来代替 init-method。 你可以试试这个:

@Bean(initMethod = "init")
public MyBean mybean() {
    return new MyBean();
}



class MyBean {
public void init() {
    System.out.println("MyBean init");
}

}

在您的 class 中,您可以声明一个名为 init() 的方法。

参考: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#:~:text=Multiple%20lifecycle%20mechanisms%20configured%20for%20the%20same%20bean% 2C%20with%20different%20initialization%20methods%2C%20are%20called%20as%20follows%3A

public class InitHelloWorld implements BeanPostProcessor {

   public Object postProcessBeforeInitialization(Object bean,
             String beanName) throws BeansException {
       System.out.println("BeforeInitialization : " + beanName);
       return bean;  // you can return any other object as well
   }

   public Object postProcessAfterInitialization(Object bean,
             String beanName) throws BeansException {
       System.out.println("AfterInitialization : " + beanName);
       return bean;  // you can return any other object as well
   }

}

暂无
暂无

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

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