spring 주해 Bean 의 생명주기 와 BeanPostProcessor

13562 단어 Spring주해 구동
bean 의 생명주기
  • bean 의 생명주기: bean 의 생 성 - 초기 화 - 소각 과정 용기 관리 bean 의 생명주기: 초기 화 와 소각 방법 을 사용자 정의 할 수 있 습 니 다.용 기 는 bean 이 현재 수명 주기 까지 진행 할 때 사용자 정의 초기 화 및 소각 방법
  • 을 호출 합 니 다.
    초기 화 및 소각 방법 지정: init - method 와 destory - method 지정
    초기 화 및 소각 방법 지정:
    @ Bean 을 통 해 init - method 와 destory - method 를 지정 합 니 다.
    bean:
    public class Car {
    
        public void init() {
            System.out.println("Car...init...");
        }
    
        public void destory() {
            System.out.println("Car...destory...");
        }
    }
    

    설정 클래스:
    @Configuration
    public class MyConfig02 {
    
        @Bean(initMethod = "init",destroyMethod = "destory")
        public Car car() {
            return new Car();
        }
    }
    
    

    테스트:
    public class MyTest01 {
        @Test
        public void test01() {
            //      
            AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MyConfig02.class);
            System.out.println("      ...");
            annotationConfigApplicationContext.close();
        }
    }
    
    

    결과: 용기 가 시 작 될 때 car 의 init 방법 을 실 행 했 고 용기 가 꺼 질 때 destory 방법 을 실 행 했 습 니 다.
    Car...init...
          ...
    Car...destory...
    

    Bean 이 InitialingBean (논리 초기 화 정의), DisposableBean (논리 폐기 정의) 을 실현 하도록 합 니 다.
    public class Cat implements InitializingBean, DisposableBean {
    
    
        public void afterPropertiesSet() throws Exception {
            System.out.println("Cat...afterPropertiesSet...");
        }
    
        public void destroy() throws Exception {
            System.out.println("Cat...destroy...");
        }
    } 
    

    JSR 250 (@ PostConstruct, @ PreDestory) 을 사용 할 수 있 습 니 다.
  • @ PostConstruct: bean 생 성 완료 및 속성 할당 완료, 초기 화 실행
  • @ PreDestory: 용기 가 bean 을 소각 하기 전에 청소 작업 을 하 라 고 알려 주세요
  • @Component
    public class Dog {
    
        public Dog() {
            System.out.println("dog...constructor");
        }
    
        @PostConstruct
        public void init() {
            System.out.println("dot...inti...");
        }
    
        @PreDestroy
        public void destory() {
            System.out.println("Dog...destory...");
        }
    }
    
    

    BeanPostProcessor
  • BeanPost Processor 를 실현 하고 2 가지 방법 을 실현 하면 모든 bean 은 초기 화 전후 에 이 2 가지 방법
  • 을 실행 합 니 다.
  • BeanPostProcessor 는 인터페이스 입 니 다. bean 의 백 엔 드 프로세서: bean 초기 화 전후 에 처리 작업 을 진행 합 니 다. 두 가지 방법 을 정 의 했 습 니 다. postProcessBeforeInitialization 초기 화 전에 작업 postProcessAfterInitialization: 초기 화 후 작업 하 는 두 가지 방법의 반환 값 은 bean 인 스 턴 스 입 니 다.
  • @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
        public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
            System.out.println("MyBeanPostProcessor...postProcessBeforeInitialization..." + "----->"+ o.getClass()+"---"+s);
            return o;
        }
    
        public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
            System.out.println("MyBeanPostProcessor...postProcessAfterInitialization..."+ "---->" + o.getClass()+"----"+s);
            return o;
        }
    }
    

    테스트 결과:
    Dog...constructor
    MyBeanPostProcessor...postProcessBeforeInitialization...----->class com.yajun.spring02.bean.Dog---dog
    Dog...inti...
    MyBeanPostProcessor...postProcessAfterInitialization...---->class com.yajun.spring02.bean.Dog----dog
    MyBeanPostProcessor...postProcessBeforeInitialization...----->class com.yajun.spring02.bean.Car---car
    Car...init...
    MyBeanPostProcessor...postProcessAfterInitialization...---->class com.yajun.spring02.bean.Car----car
    MyBeanPostProcessor...postProcessBeforeInitialization...----->class com.yajun.spring02.bean.Cat---cat
    Cat...afterPropertiesSet...
    MyBeanPostProcessor...postProcessAfterInitialization...---->class com.yajun.spring02.bean.Cat----cat
    
       05, 2019 3:22:41    org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
      : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@afffde: startup date [Wed Jun 05 15:22:40 CST 2019]; root of context hierarchy
          ...
    Cat...destroy...
    Car...destory...
    Dog...destory...
    
    

    spring 바 텀 에서 BeanPostProcessor 에 대한 사용: bean 의 할당, 다른 구성 요소 주입, @ Autowired, 수명 주기 주석 기능,

    좋은 웹페이지 즐겨찾기