프로그래밍/- Spring Runner 교육

# 13-2 빈의 생명주기 확장

즐겁게 하하하 2022. 11. 2. 18:25
728x90

빈의 생명주기 확장 : Lifecycle Callbacks 인터페이스
 - 빈의 의존관계 주입이 끝나고 호출하는 콜백 인터페이스

InitializingBean :   'Bean'이 생성될 때 마다 호출되는 메서드
DisposableBean  :  'Bean'이 소멸될 때 마다 호출되는 메서드

jsr 250 - 자주 사용되는 여러 애너테이션 기록

  • @PostConstruct 는 WAS 가 뜰 때 bean이 생성된 다음 딱 한번만 실행된다.
  • @PreDestroy 는 컨테이너에서 객체를 제거하기 전에 실행된다.
dependencies { // JAXB에 대한 의존성 추가 > JAXB 는 XML 문서와 자바 객체를 서로 매핑해 준다.
	implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.1'
	
	// Junit 의존성 추가
	implementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
	
	// Junit 코드 실행 런처
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.5.1'
	
	// spring context
	implementation 'org.springframework:spring-context:5.3.9'
	 
	// spring context support
	implementation 'org.springframework:spring-context-support:5.3.9'
	
	// 테스트 컨텍스트
	testImplementation 'org.springframework:spring-test:5.3.9'
	
	// 로그백
	implementation 'ch.qos.logback:logback-classic:1.2.3'
	
	//OXM
	implementation 'org.springframework:spring-oxm:5.3.9'
	
	//jsr 250
	implementation 'javax.annotation:javax.annotation-api:1.3.2'
}
	@PostConstruct
	public void afterPropertiesSet() throws Exception {
		
		// 존재하는 파일인지 검증코드 추가
		URL metadataUrl = ClassLoader.getSystemResource(metadata);
		if(Objects.isNull(metadataUrl)) {
			throw new FileNotFoundException();
		}
		
		// 읽어 들일수 있는 파일인지 검증
		if( Files.isReadable(Path.of(metadataUrl.toURI())) == false ) {
			throw new ApplicationException(String.format("cannot read to metadata. [%s]", metadata));
		}
	}

	@PreDestroy
	public void destroy() throws Exception {
		log.info("Destoryed bean");	
	}
728x90