【Spring源码解析】生命周期-@PostConstruct&@PreDestroy
    
  
      
      
     
    
      
         前言
请注意,@PostConstruct和@PreDestroy注释都是Java EE的一部分。而且由于Java EE在Java
9中已被弃用,而在Java 11中已被删除,因此我们必须添加一个附加依赖项才能使用这些注释
 实现
1 2 3 4 5
   | <dependency>     <groupId>javax.annotation</groupId>     <artifactId>javax.annotation-api</artifactId>     <version>1.3.2</version> </dependency>
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | @Component public class Dog {
      public Dog() {         System.out.println("dog constructor...");     }
      @PostConstruct     public void init() {         System.out.println("dog init...");     }
      @PreDestroy     public void destroy() {         System.out.println("dog destroy...");     } }
   | 
 
1 2 3 4 5 6 7 8 9
   | @ComponentScan("cn.idea360.bean") @Configuration public class BeanConfigOfLifeCycle {
      @Bean(initMethod = "init", destroyMethod = "destroy")     public Car car() {         return new Car();     } }
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | 
 
 
 
 
 
 
 
 
 
 
  @Test public void test07() {     AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfigOfLifeCycle.class);     System.out.println("容器创建完成");
           applicationContext.close(); }
 
  | 
 
 最后
本篇到此结束,欢迎大家关注公众号【当我遇上你】。