Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 스프링
- 인터페이스
- @Bean
- Spring 개발환경 설정
- spring
- 자동주입
- 객체
- 영속성 컨텍스트
- 리액트
- react
- 자바
- mysql
- java
- 상속
- 스프링 컨테이너
- 트랜잭션
- merge
- pom.xml
- @transactional
- DI
- 이클립스
- JVM
- 어노테이션
- Spring legacy Project
- 빈
- list
- 깃허브
- springboot
- github
- 의존성주입
Archives
- Today
- Total
DEVLOG
생명주기(Life Cycle) 본문
스프링 컨테이너 생명주기
[생성 시점]
- 빈(Bean) 객체도 함께 생성된다.
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext("classpath:appCtx.xml");
[소멸 시점]
- 빈(Bean) 객체도 함께 소멸된다. (<- 메모리에서 사라진다는 뜻)
ctx.close();
빈(Bean) 객체 생명주기 (= 스프링 컨테이너의 생명주기)
[생성 시점]
- 스프링 컨테이너 초기화 시 빈(Bean) 객체 생성 및 주입
[소멸 시점]
- 스프링 컨테이너 종료 시 빈(Bean) 객체 소멸
빈(Bean) 객체가 생성되고 소멸되는 시점에 특정한 작업 할 수 있는 방법
1. 인터페이스 활용 (implement)
사용방법 : 해당 빈(Bean) 객체의 클래스에서 스프링 프레임워크가 제공하는 initalizingBean과 DisposableBean 인터페이스를 상속받는다. 그리고 Add unimplemented methods 후 메소드 실행문 부분에 작업하고자 하는 내용을 적어준다.
InitializingBean | disposableBean |
빈(Bean) 객체 생성시점에 호출 afterPropertiesSet() 메소드 |
빈(Bean) 객체 소멸시점에 호출 destroy() 메소드 |
BookRegisterService.java
public class BookRegisterService implements InitializingBean, DisposableBean { // 인터페이스
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean 객체 생성");
}
@Override
public void destroy() throws Exception {
System.out.println("Bean 객체 소멸");
}
...
}
만약 생성되는 시점에만 특정 작업을 하고 싶다면 implements InitializingBean과 afterPropertiesSet() 메소드만 추가하면 된다.
2. init-method, destroy-method 속성
사용방법 : 스프링 컨테이너 빈(Bean) 객체의 init-method과 destroy-method 속성의 값과 동일한 메소드명으로 해당 클래스에 메소드를 선언한다.
appCtx.xml
- 해당 빈(Bean) 객체의 init-method와 destory-method 속성과 값이 부여되어 있다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- @Autowired 어노테이션 사용 위해 아래 코드 명시해줘야 함-->
<context:annotation-config />
<!-- 객체 생성 -->
<bean id="bookDao" class="com.brms.book.dao.BookDao" />
<!--
빈(Bean) 객체가 생성되고 소멸되는 시점에 특정한 작업 할 수 있는 방법 중
init-method, destroy-method 방법은 빈(Bean) 객체 속성의 init-method명, destroy-method명과 동일한 메소드명으로
해당 클래스에 선언되어야 함 -->
<bean id="bookRegisterService" class="com.brms.book.service.BookRegisterService"
init-method="initMethod" destroy-method="destroyMethod"/>
<bean id="bookSearchService" class="com.brms.book.service.BookSearchService" />
<bean id="memberDao" class="com.brms.member.dao.MemberDao" />
<bean id="memberRegisterService" class="com.brms.member.service.MemberRegisterService" />
<bean id="memberSearchService" class="com.brms.member.service.MemberSearchService" />
</beans>
BookRegisterService.java
- 스프링 컨테이너의 객체 속성인 init-method와 destroy-method의 값과 동일한 메소드명으로 메소드를 선언한다.
package com.brms.book.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.brms.book.Book;
import com.brms.book.dao.BookDao;
public class BookRegisterService{
// 스프링 컨테이너에서 동일한 데이터타입 찾아서 자동 주입
@Autowired
private BookDao bookDao;
// 프로퍼티에 @Autowired 어노테이션 사용하려면 디폴트 생성자 명시되어야 함
public BookRegisterService() { }
public void register(Book book) {
bookDao.insert(book); // 자동 주입된 객체 bookDao
}
// 스프링 컨테이너의 객체 속성 init-method destroy-method의 값과 동일한 메소드명 지어야함
public void initMethod() {
System.out.println("BookRegisterService 빈(Bean)객체 생성 단계");
}
public void destroyMethod() {
System.out.println("BookRegisterService 빈(Bean)객체 소멸 단계");
}
}
출처
자바 스프링 프레임워크(renew ver.) - 신입 프로그래머를 위한 강좌 - 인프런 | 강의
스프링 프레임워크 기본부터 실전 사용법까지! 충실하고 폭넓은 설명과 예제를 통해 현장에 바로 투입되어 활약하는 개발자로 거듭나세요., - 강의 소개 | 인프런...
www.inflearn.com
'Spring' 카테고리의 다른 글
어노테이션을 이용한 스프링 설정 - 2 (0) | 2022.06.08 |
---|---|
어노테이션을 이용한 스프링 설정 - 1 (0) | 2022.06.08 |
의존객체 선택 (0) | 2022.06.07 |
의존객체 자동 주입 (0) | 2022.06.07 |
스프링 설정 파일 분리 (0) | 2022.06.06 |
Comments