JAVA
1.0.0
1.0.0
  • README
  • Git
    • Basic
    • Remote Repository
    • Log & Diff
    • Rebase&Cherri-Pick
    • git-flow
  • DevOps
    • Monolithic vs MSA
    • Jenkins 시작하기
    • Airflow 시작하기
    • Airflow 시작하기
    • Build Tools
      • maven
  • 개발 방법론
    • TDD
  • Spring
    • IoC
    • Is Spring Bean Thread-Safe?
    • Spring Singleton
    • Component Scan
    • Spring Annotation
    • 의존 관계 주입(DI)
    • Lombok 활용하기
    • Bean 생명주기와 콜백
    • Bean Scope
    • AOP(1) - AOP란
    • AOP(2) - Aop Proxy
    • AOP(3) - Dynamic Proxy
    • AOP(4) - AspectJ
    • POJO
    • Spring 서비스 구조
    • Transaction
    • JPA란?
    • JPA Entity
    • Spring Data JPA
    • Spring Data Specification
    • Model Mapping
    • Cache
    • restTemplate
    • YAML 파일 설정
    • Spring Boot
      • H2 DB 설정
      • 다중 데이터베이스 설정
      • Mybatis 연동하기
    • Spring Batch
      • Batch 시작해보기
      • Batch Job Flow
      • Job
      • Step
      • Batch Scope & Job Parameter
      • JobRepository와 메타테이블
      • Chunk 지향 프로그래밍
      • ItemReader
      • ItemProcessor
      • ItemWriter
      • Batch Schedular
      • Job별 Bean등록하기
      • Batch 구현시 발생한 오류 정리
      • Spring Batch Scaling
        • Multithread Job구현시 이슈사항
    • Spring test
      • Junit5
        • 테스트 이름 표기
        • 테스트 그룹 사이의 관계
        • 태그와 필터링
        • 동적 테스트
        • 테스트 LifeCycle
        • 테스트 메서드
        • 테스트 순서
        • AssertJ
        • 테스트 병렬 실행
        • AssertJ
        • Mock
      • Spring Boot Test DB 분리
      • Spring Batch Test
  • Web Application
    • Web Server & WAS
    • 관련 개념 - HTTP API, HTML, CSR, SSR
    • Servlet
    • JSP
    • Cookie And Session
    • 예외페이지
    • Java Bean
    • JDBC
    • Connection Pool
    • 파일 업로드
    • Expression Language
    • JSTL
    • FrontController패턴 Command 패턴
    • Forwarding
    • MVC
    • 회원가입예제
    • 참고
      • 개발환경설정
  • Java+
    • SOAP/WSDL vs REST
    • WSDL을 JAVA로 변환하기
    • SOAP 통신 OPEN API로 개발해보기
  • Java
    • Basic
      • 변수와 타입
      • 연산자
      • 조건문과 반복문
      • 참조 타입
      • 클래스
      • 상속(Inheritance)
      • 인터페이스(Interface)
      • 중첩 클래스와 중첩 인터페이스
      • 예외 처리
      • API - Object, System, Class, Math, Wrapper
      • API - String, StringBuffer, StringBuilder
      • Thread
      • Generic
      • Lambda
      • Collection - List, Set
      • Collection - Map
      • Collection - Tree
      • Collection - Stack, Queue
      • Stream
      • Reflection
      • 정규표현식
      • GUI
      • UML
      • Serializable
    • Advanced
      • OutOfMemoryError
      • AutoValue
      • meta-annotation
        • @Retention
        • @Target
        • @Repeatable
    • Effective Java 3/E
      • ITEM 1: Static Factory Method(정적 메소드)
      • ITEM 2: Builder Pattern
      • ITEM 3: Singleton
      • ITEM 4: Private Constructor
      • ITEM 5: Dependency Injection
      • ITEM 6: Avoid Unnecessary Object
      • ITEM 7: Eliminate Object Reference
      • ITEM 8: Avoid finalizer and cleaner
      • ITEM 9: try-with-resources
      • ITEM 10: The gerneral contract when overriding equlas
      • ITEM 11: Overriding hashCode
      • ITEM 12: overriding toString
      • ITEM 13: overriding clone judiciously
      • ITEM 14: Consider implementing comparable
      • ITEM 15: 클래스와 멤버의 접근을 최소화해라
      • ITEM 16: Use Accessor methods
      • ITEM 17: 변경 가능성을 최소화해라(불변 클래스)
      • ITEM 18: 상속보단 컴포지션을 사용해라
      • ITEM 19: 상속을 고려해 설계하고 문서화해라
      • ITEM 20: 추상 클래스보다 인터페이스를 우선하라
      • ITEM 21: 인터페이스는 구현하는 쪽을 생각해 설계해라.
      • ITEM 22: 인터페이스는 타입을 정의하는 용도로만 사용해라
      • ITEM 23: 태그 달린 클래스보다 클래스 계층구조를 활용해라
      • ITEM 24: 멤버 클래스는 되도록 static으로 구현해라
      • ITEM 25: 톱레벨 클래스는 한 파일에 하나만 생성해라.
      • ITEM 26: Raw type은 사용하지 마라
      • ITEM 27: 비검사 경고를 제거해라
      • ITEM 28: 배열보다는 리스트를 사용해라
      • ITEM 29: 이왕이면 제네릭 타입으로 만들어라
      • ITEM 30: 이왕이면 제네릭 메서드로 만들어라
      • ITEM 31 : 한정적 와일드카드를 사용해 API 유연성을 높여라
      • ITEM 32: 제네릭과 가변인수를 함께 쓸 때는 신중해라
      • ITEM 33: 타입 안전 이종 컨테이너를 고려해라
      • ITEM 34: int 상수 대신 열거 타입을 사용해라
      • ITEM 35: ordinal 메서드 대신 인스턴스 필드를 사용해라
      • ITEM 36: 비트 필드 대신 EnumSet을 사용해라
      • ITEM 37: ordinal 인덱싱 대신 EnumMap을 사용해라
      • TEM 38 : 확장할 수 있는 열거타입이 필요하면 인터페이스를 사용해라
      • ITEM 39: 명명 패턴보다 애너테이션을 사용해라
      • ITEM 40: @Override 어노테이션을 일관되게 사용해라
      • ITEM 41: 정의하려는 것이 타입이라면 마커 인터페이스를 사용해라
      • ITEM 42: 익명 클래스보다는 람다를 사용해라
      • ITEM 43: 람다보다는 메서드 참조를 사용해라
      • ITEM 44: 표준 함수형 인터페이스를 사용해라
      • ITEM 45: 스트림은 주의해서 사용해라
      • ITEM 46: 스트림에서 부작용 없는 함수를 사용해라
      • ITEM 47: 반환 타입으로는 스트림보다 컬렉션이 낫다.
      • ITEM 48: 스트림 병렬화는 주의해서 사용해라
      • ITEM 49: 매개변수가 유효한지 검사해라
      • ITEM 50: 적시에 방어적 복사본을 만들어라
      • ITEM 51: 메서드 시그니처를 신중히 설계해라
      • ITEM 52: 다중정의는 신중히 사용해라
      • ITEM 53: 가변인수는 신중히 사용해라
      • ITEM 54: null이 아닌, 빈 컬렉션이나 배열을 반환해라
      • ITEM 55: Optional 반환은 신중하게 해라
      • ITEM 56: 공개된 API 요소에는 항상 주석을 작성해라
      • ITEM 57: 지역변수의 범위를 최소화해라
      • ITEM 58: 전통적인 for 문보다는 for-each문을 사용해라
      • ITEM 59: 라이브러리를 익히고 사용해라
      • ITEM 60: 정확한 답이 필요하다면 float와 double은 피해라
      • ITEM 61: 박싱된 기본 타입보다는 기본 타입을 사용해라
      • ITEM 62: 다른 타입이 적절하다면 문자열 사용을 피해라
      • ITEM 63: 문자열 연결은 느리니 주의해라
      • ITEM 64: 객체는 인터페이스를 사용해 참조해라
      • ITEM 65: 리플렉션보다는 인터페이스를 사용해라
      • ITEM 66: 네이티브 메서드는 신중히 사용해라
      • ITEM 67: 최적화는 신중히 해라
      • ITEM 68: 일반적으로 통용되는 명명 규칙을 따라라
    • 객체지향 설계 원칙(SOLID)
    • 디자인패턴
      • Strategy Pattern
      • Template Method Pattern
      • Factory Method Pattern
      • Singleton
      • Delegation
      • Proxy
      • Adapter Pattern
    • 실습
      • 인터페이스 실습 - Vehicle
      • 인터페이스 실습 - Remote
      • GUI 실습 - Calculator
      • GUI 실습 - button
      • GUI 실습 - lotto
      • Thread 실습 - 좌석예약, 메세지보내기
    • Jar vs War
  • 데이터베이스
    • KEY
    • Index
    • Transaction
    • Trigger
    • Procedure / Function
    • Package
    • 데이터베이스 배움터
      • 데이터베이스 시스템
      • 관계데이터 모델
      • 관계대수와 SQL
    • MySQL
      • Database란
      • MySQL 시작하기
      • MySQL Database
      • MySQL Table
      • CRUD
      • 관계형 데이터베이스
      • Server와 Client
    • PostgreSQL
    • NoSQL
      • Install Cassandra on mac
      • Cassandra란?
      • NiFi란
  • Algorithm
    • String
    • Recursion
    • Dynamic Programming
    • Array, Struct, Pointer
    • Math
    • Sort
    • List
    • Stack
    • Queue
    • Graph
    • Tree
    • Maze
    • AVL
    • 이진탐색트리(Binary Search Tree)
    • DFS와 BFS
    • 다익스트라 알고리즘(Dijkstra's Algorithm)
    • Red-Black 트리
    • A* 알고리즘
    • Heap
    • Huffman Coding
    • Priority Queue
    • Bellman-Ford 알고리즘
    • C++
      • Class
      • STL
        • STL pair
        • STL Container - Associate Container
        • STL Container - Sequence Container
        • STL Container - Container Adapter
  • JavaScript
    • JABASCRIPT BASIC
    • Shallow Copy vs Deep Copy
    • OBJECT MODEL
    • NODE
    • 동기 처리 vs 비동기 처리
    • AJAX
    • CALLBACK
    • PROMISE
    • DEFERRER
    • UNDERSCORE
    • WEBPACK
    • SCOPE
    • EXECUTION CONTEXT
    • Image Object
    • BFCache란?
    • history.scrollRestoration
    • Intersection Observer
    • JWT - JSON Web Token
    • HTML vs JSON
  • Vue.js
    • 환경설정
    • Vue.js란?
    • Vue Instance
    • Vue Component
    • Vue Router
    • HTTP 통신
    • Template
    • Single File Component
    • Vue Animation
    • Vuex
    • Djnago와 연동하기
  • Backbone.js
    • Model
    • Collection
    • Sync
    • view
  • Node.js
    • Doit! - 노드로 만들 수 있는 대표적인 서버와 용도
    • Doit! - 노드에 대해 알아보고 개발 도구 설치하기
    • Doit! - 노드 간단하게 살펴보기
    • Doit! - 노드의 자바스크립트와 친해지기
    • Doit! - 노드의 기본 기능 알아보기
    • Doit! - 웹 서버 만들기
    • Doit! - 데이터베이스 사용하기
    • Doit! - 익스프레스 프로젝트를 모듈화하기
    • Doit! - 뷰 템플릿 적용하기
    • Doit! - 패스포트로 사용자 인증하기
    • Doit! - 채팅서버 만들기
    • Doit! - JSON-RPC 서버 만들기
  • Python
    • Warning-Could not import the lzma module
    • Pandas
      • Pandas 자료구조
      • Pandas 데이터 입출력
      • DataFrame Data 살펴보기
      • 시각화 도구 - Matplotlib
  • ML
    • 추천 시스템
      • Collaborative Filtering
      • Matrix Factorization
  • Django
    • Basic
      • 환경설정
      • About Django
      • Start Django Project
      • Secret Key 관리하기
      • Settings 분리하기
      • Django App
      • Django View & URL (1)
      • Django Model
        • MySQL 연동
      • Django Admin
      • Django View & URL (2)
      • Django Template
      • Django Template & View & URL
      • Django Static
      • Django form
    • Advanced
      • Django Generic View
      • Django Automated Testing
      • Django Extenstion Template
      • Django Model Package
      • Django OpenSSL setting
    • REST framework
      • Rest API
      • Serializers
      • ViewSet
    • Error
      • 환경설정 zlib 오류발생
      • ModuleNotFoundError
    • 패키지
      • django-debug-toolbar
    • Vue.js 연동하기
  • Ruby
    • variable & input/output
    • 조건문
    • 반복문
    • Array & Hash
    • Method
    • Proc&Lamda
    • Class
  • Ruby on Rails
    • Scaffolding
    • Controller
    • Model
    • Model-M:N relation
    • Model Validation
    • 멋사 10주차 수업(Tip)
  • HTML/CSS
    • Udacity - Intro to HTML/CSS
    • Udacity - Responsive Web Design
    • Udacity - Responsive Images
    • HTML Basic
    • CSS Basic
    • HTML5 Sementic Tag
    • HTML 텍스트 관련 태그들
    • HTML5 멀티미디어
    • HTML 폼 관련 태그들
    • 텍스트 관련 스타일
    • 색상과 배경을 위한 스타일
    • 레이아웃을 위한 스타일
    • CSS 포지셔닝
    • 다재다능한 CSS3 선택자
    • CSS와 애니메이션
    • 반응형 웹이란?
  • OS(운영체제)
    • Linux
      • Daemon
      • Cron
      • 프로세스 관련 명령어
      • 텍스트 파일 명령어
  • Network
    • 네트워크 기본 개념
    • 네트워크 기본 규칙
    • 물리 계층
    • 데이터 링크 계층
    • 네트워크 계층
    • 전송 계층
    • 응용 계층
    • 네트워크 전체 흐름
    • 무선 랜
  • IT 기타지식
    • NAS란
Powered by GitBook
On this page
  • 콜백 방법
  • 인터페이스 방법
  • 설정 정보 사용(메서드 등록)
  • 어노테이션 방법
  • 참고

Was this helpful?

  1. Spring

Bean 생명주기와 콜백

PreviousLombok 활용하기NextBean Scope

Last updated 3 years ago

Was this helpful?

스프링 빈은 객체생성 -> 의존관계주입의 라이프사이클을 가진다. (생성자 주입의 경우 예외)

스프링 빈은 객체를 생성하고, 의존관계 주입이 다 끝난 후에 필요한 데이터를 사용할 수 있는 준비가 완료된다. 따라서 의존관계 주입이 모두 완료되고 난 후에 초기화가 호출되야한다.

  • 스프링은 의존관계 주입이 완료되면 스프링 빈에 콜백 메서드를 통해서 초기화 시점을 알려주는 다양한 기능을 제공한다.

  • 스프링 컨테이너가 종료되기 직전에 소멸 콜백을 준다.

스프링 빈 이벤트 라이프사이클

  1. 스프링 컨테이너 생성

  2. 스프링 빈 생성

  3. 의존관계 주입

  4. 초기화 콜백 : 빈이 생성되고, 빈의 의존관계 주입이 완료된 후

  5. 사용

  6. 소멸전 콜백 : 빈이 소멸되기 직전에 호출

  7. 스프링 종료

생성자는 필수 정보를 받고, 메모리를 할당해서 객체를 생성하는 책임을 가진다.

초기화는 이렇게 생성된 값을 활용해서 외부 커넥션을 연결하는 등 무거운 동작을 수행한다.

즉, 생성자 안에서 초기화 작업을 함께 하는 것 보다는 객체를 생성하는 부분과 초기화 하는 부분은 명확하게 나누는 것이 유지보수 관점에 좋다.

콜백 방법

인터페이스 방법

InitializingBean, DisposableBean 을 구현하여, 초기화 메서드와 소멸 메서드를 설정할 수 있다.

  • InitializingBean

    public interface InitializingBean {
    
    	/**
    	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
    	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
    	 * <p>This method allows the bean instance to perform validation of its overall
    	 * configuration and final initialization when all bean properties have been set.
    	 * @throws Exception in the event of misconfiguration (such as failure to set an
    	 * essential property) or if initialization fails for any other reason
    	 */
    	void afterPropertiesSet() throws Exception;
    
    }
  • DisposableBean

    public interface DisposableBean {
    
    	/**
    	 * Invoked by the containing {@code BeanFactory} on destruction of a bean.
    	 * @throws Exception in case of shutdown errors. Exceptions will get logged
    	 * but not rethrown to allow other beans to release their resources as well.
    	 */
    	void destroy() throws Exception;
    
    }
  • 예제 클래스

    package dh0023.springcore.lifecycle;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    /**
     * 테스트를 위한 가짜 NetworkClient
     */
    public class NetworkClient implements InitializingBean, DisposableBean {
    
        private String url;
    
        public NetworkClient() {
            System.out.println("생성자 호출, url = " + url);
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        // start service
        public void connect() {
            System.out.println("connect: " + url);
        }
    
        public void call(String message) {
            System.out.println("call: " + url + " message = " + message);
        }
    
        public void disconnect() {
            System.out.println("close: " + url);
        }
    
        /**
         * 의존관계 주입 후 호출
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("NetworkClient.afterPropertiesSet");
            connect();
            call("초기화 연결 메세지");
        }
    
        /**
         * 소멸직후 콜백
         * @throws Exception
         */
        @Override
        public void destroy() throws Exception {
            System.out.println("NetworkClient.destroy");
            disconnect();
        }
    }
  • Test Code

    package dh0023.springcore.lifecycle;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Description;
    
    public class BeanLifeCycleTest {
    
        @Test
        @Description("인터페이스 적용 테스트")
        void lifeCycleInterfaceTest() {
            ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
            NetworkClient client = ac.getBean(NetworkClient.class);
            ac.close();
        }
    
    
        @Configuration
        static class LifeCycleConfig {
            @Bean
            public NetworkClient networkClient() {
                NetworkClient networkClient = new NetworkClient();
                networkClient.setUrl("http://spring-core.dev");
                return networkClient;
            }
        }
    }
  • 결과

    23:23:39.553 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'networkClient'
    생성자 호출, url = null
    NetworkClient.afterPropertiesSet
    connect: http://spring-core.dev
    call: http://spring-core.dev message = 초기화 연결 메세지
    23:23:39.645 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@932bc4a, started on Sun May 16 23:23:38 KST 2021
    NetworkClient.destroy
    close: http://spring-core.dev
    
    Process finished with exit code 0
    1. Creating shared instance of singleton bean 'networkClient'

    2. 생성자 호출

    3. NetworkClient.afterPropertiesSet

    4. Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@932bc4a

    5. NetworkClient.destroy

    6. 스프링 종료

    다음과 같은 순서로 사이클이 수행되는 것을 확인할 수 있다.

단점

  • 스프링 전용 인터페이스이다. 해당 코드가 스프링 전용 인터페이스에 의존하게 된다.

  • 초기화, 소멸 메서드의 이름을 변경할 수 없다.

  • 외부 라이브러리에 적용할 수 없다.

이 방법은 초창기 나온 방법으로, 현재는 거의 사용하지 않는다.

설정 정보 사용(메서드 등록)

@Bean 어노테이션에 등록 초기화, 소멸 메서드를 설정하는 방법이다.

  • 예제 클래스

    /**
     * 테스트를 위한 가짜 NetworkClient
     */
    public class NetworkClientMethod {
    
        private String url;
    
        public NetworkClientMethod() {
            System.out.println("생성자 호출, url = " + url);
    
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        // start service
        public void connect() {
            System.out.println("connect: " + url);
        }
    
        public void call(String message) {
            System.out.println("call: " + url + " message = " + message);
        }
    
        public void disconnect() {
            System.out.println("close: " + url);
        }
    
        public void init() {
            System.out.println("NetworkClientMethod.init");
            connect();
            call("초기화 연결 메세지");
    
        }
    
        public void close() {
            System.out.println("NetworkClientMethod.close");
            disconnect();
        }
    
    }
  • 테스트 코드

    package dh0023.springcore.lifecycle;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Description;
    
    public class BeanLifeCycleTest {
    
        @Test
        @Description("메서드 적용 테스트")
        void lifeCycleMethodTest() {
            ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
            NetworkClientMethod client = ac.getBean(NetworkClientMethod.class);
            ac.close();
        }
    
    
        @Configuration
        static class LifeCycleConfig {
    
            @Bean(initMethod = "init", destroyMethod = "close")
            public NetworkClientMethod networkClientMethod() {
                NetworkClientMethod networkClientMethod = new NetworkClientMethod();
                networkClientMethod.setUrl("http://spring-core.dev");
                return networkClientMethod;
            }
        }
    }
  • 결과

    23:35:00.964 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'networkClientMethod'
    생성자 호출, url = null
    NetworkClientMethod.init
    connect: http://spring-core.dev
    call: http://spring-core.dev message = 초기화 연결 메세지
    23:35:01.128 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@932bc4a, started on Sun May 16 23:35:00 KST 2021
    NetworkClientMethod.close
    close: http://spring-core.dev
    
    Process finished with exit code 0
    1. Creating shared instance of singleton bean 'networkClientMethod'

    2. 생성자 호출

    3. 초기화 콜백 : NetworkClientMethod.init

    4. Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@932bc4a

    5. 소멸직후 콜백 : NetworkClientMethod.close

    6. 스프링 종료

특징

  • 메서드 이름을 자유롭게 줄 수 있다.

  • 스프링 빈이 스프링 코드에 의존하지 않는다.

  • 설정 정보를 사용하기 때문에 코드를 고칠 수 없는 외부 라이브러리에도 초기화, 종료 메서드를 적용할 수 있다.

@Bean destroyMethod

라이브러리 대부분 close, shutdown 과 같은 이름의 종료 메서드를 사용한다.

	String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
public static final String INFER_METHOD = "(inferred)";

destoryMethod() 의 default값은 (inferred) 로 등록되어 있다. 이 추론 기능은 close, shutdown 과 같은 이름의 메서드를 자동으로 호출해준다. 이름 그대로 종료 메서드를 추론해서 호출해주는 것이다.

따라서 직접 스프링 빈으로 등록하면 종료 메서드는 따로 적어주지 않아도 된다. 만약 추론기능을 사용하기 싫은 경우에는 destroyMethod="" 로 설정해주면 된다.

어노테이션 방법

  • @PostConstruct

    package javax.annotation;
    
    import java.lang.annotation.*;
    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.RetentionPolicy.*;
    
    /**
     * The <code>PostConstruct</code> annotation is used on a method that 
     * needs to be executed after dependency injection is done to perform 
     * any initialization. This  method must be invoked before the class 
     * is put into service. This annotation must be supported on all classes 
     * that support dependency injection. The method annotated with 
     * <code>PostConstruct</code> must be invoked even if the class does 
     * not request any resources to be injected. Only one 
     * method in a given class can be annotated with this annotation. 
     * The method on which the <code>PostConstruct</code> annotation is 
     * applied must fulfill all of the following criteria:
     * <ul>
     * <li>The method must not have any parameters except in the case of 
     * interceptors in which case it takes an <code>InvocationContext</code>
     * object as defined by the Interceptors specification.</li>
     * <li>The method defined on an interceptor class or superclass of an
     * interceptor class must have one of the following signatures:
     * <p>
     * void &#060;METHOD&#062;(InvocationContext)
     * <p>
     * Object &#060;METHOD&#062;(InvocationContext) throws Exception
     * <p>
     * <i>Note: A PostConstruct interceptor method must not throw application 
     * exceptions, but it may be declared to throw checked exceptions including 
     * the java.lang.Exception if the same interceptor method interposes on 
     * business or timeout methods in addition to lifecycle events. If a 
     * PostConstruct interceptor method returns a value, it is ignored by 
     * the container.</i>
     * </li>
     * <li>The method defined on a non-interceptor class must have the 
     * following signature:
     * <p>
     * void &#060;METHOD&#062;()
     * </li>
     * <li>The method on which the <code>PostConstruct</code> annotation
     * is applied may be public, protected, package private or private.</li>
     * <li>The method must not be static except for the application client.</li>
     * <li>The method should not be final.</li>
     * <li>If the method throws an unchecked exception the class must not be put into   
     * service except in the case where the exception is handled by an
     * interceptor.</li></ul>
     *
     * @see javax.annotation.PreDestroy
     * @see javax.annotation.Resource
     * @since 1.6, Common Annotations 1.0
     */
    @Documented
    @Retention (RUNTIME)
    @Target(METHOD)
    public @interface PostConstruct {
    }
  • @PreDestroy

    package javax.annotation;
    
    import java.lang.annotation.*;
    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.RetentionPolicy.*;
    
    /**
     * The <code>PreDestroy</code> annotation is used on a method as a
     * callback notification to signal that the instance is in the
     * process of being removed by the container. The method annotated
     * with <code>PreDestroy</code> is typically used to
     * release resources that it has been holding. This annotation must be
     * supported by all container-managed objects that support the use of
     * the <code>PostConstruct</code> annotation except the Jakarta EE application 
     * client. The method on which the <code>PreDestroy</code> annotation
     * is applied must fulfill all of the following criteria:
     * <ul>
     * <li>The method must not have any parameters except in the case of
     * interceptors in which case it takes an <code>InvocationContext</code>
     * object as defined by the Interceptors specification.</li>
     * <li>The method defined on an interceptor class or superclass of an
     * interceptor class must have one of the following signatures:
     * <p>
     * void &#060;METHOD&#062;(InvocationContext)
     * <p>
     * Object &#060;METHOD&#062;(InvocationContext) throws Exception
     * <p>
     * <i>Note: A PreDestroy interceptor method must not throw application
     * exceptions, but it may be declared to throw checked exceptions including
     * the java.lang.Exception if the same interceptor method interposes on
     * business or timeout methods in addition to lifecycle events. If a
     * PreDestroy interceptor method returns a value, it is ignored by
     * the container.</i>
     * </li>
     * <li>The method defined on a non-interceptor class must have the
     * following signature:
     * <p>
     * void &#060;METHOD&#062;()
     * </li>
     * <li>The method on which PreDestroy is applied may be public, protected,
     * package private or private.</li>
     * <li>The method must not be static.</li>
     * <li>The method should not be final.</li>
     * <li>If the method throws an unchecked exception it is ignored by
     * the container.</li>
     * </ul>
     *
     * @see javax.annotation.PostConstruct
     * @see javax.annotation.Resource
     * @since 1.6, Common Annotations 1.0
     */
    
    @Documented
    @Retention (RUNTIME)
    @Target(METHOD)
    public @interface PreDestroy {
    }
  • 테스트 클래스

    package dh0023.springcore.lifecycle;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    /**
     * 테스트를 위한 가짜 NetworkClient
     */
    public class NetworkClient {
    
        private String url;
    
        public NetworkClient() {
            System.out.println("생성자 호출, url = " + url);
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        // start service
        public void connect() {
            System.out.println("connect: " + url);
        }
    
        public void call(String message) {
            System.out.println("call: " + url + " message = " + message);
        }
    
        public void disconnect() {
            System.out.println("close: " + url);
        }
    
        /**
         * 의존관계 주입 후 호출
         * @throws Exception
         */
        @PostConstruct
        public void init() throws Exception {
            System.out.println("NetworkClient.init");
            connect();
            call("초기화 연결 메세지");
        }
    
        /**
         * 소멸직전 콜백
         * @throws Exception
         */
        @PreDestroy
        public void close() throws Exception {
            System.out.println("NetworkClient.close");
            disconnect();
        }
    }
  • 테스트 코드

    package dh0023.springcore.lifecycle;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Description;
    
    public class BeanLifeCycleTest {
        @Test
        @Description("어노테이션 방 적용 테스트")
        void lifeCycleTest() {
            ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
            NetworkClient client = ac.getBean(NetworkClient.class);
            ac.close();
        }
    
    
        @Configuration
        static class LifeCycleConfig {
            @Bean
            public NetworkClient networkClient() {
                NetworkClient networkClient = new NetworkClient();
                networkClient.setUrl("http://spring-core.dev");
                return networkClient;
            }
        }
    }
  • 결과

    23:50:43.425 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'networkClient'
    생성자 호출, url = null
    NetworkClient.init
    connect: http://spring-core.dev
    call: http://spring-core.dev message = 초기화 연결 메세지
    23:50:43.557 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@932bc4a, started on Sun May 16 23:50:42 KST 2021
    NetworkClient.destroy
    close: http://spring-core.dev

특징

  • 최신 스프링에서 가장 권장하는 방법

  • 애노테이션만 붙이면 되므로 매우 편리

  • javax.annotation 패키지는 스프링 종속적인 기술이 아니라 자바 표준이다. 따라서, 스프링이 아닌 다른 컨테이너에서도 동작한다.

단점

  • 외부 라이브러리에 적용하지 못한다. 외부 라이브러리를 초기화/종료 해야하면 @Bean 의 기능을 사용해야한다.

참고

김영한 스프링 핵심 원리 - 기본편
https://media.geeksforgeeks.org/wp-content/uploads/20200428011831/Bean-Life-Cycle-Process-flow3.png