spring 주해 Bean 의 생명주기 와 BeanPostProcessor
초기 화 및 소각 방법 지정: 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) 을 사용 할 수 있 습 니 다.
@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
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, 수명 주기 주석 기능,
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.