Spring - 컨테이너 생명주기, Bean 생명주기
프로그래밍 정리/Spring
2019. 12. 9. 10:39
반응형
Spring - 컨테이너 생명주기, Bean 생명주기
-컨테이너 생명주기
XML을 담아두는 컨테이너 설정이 가능하다.
1. 생성
xml을 포함하지않고 단순 객체 선언
1 | GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); | cs |
2. 설정
원하는 xml파일 연결
★★중요★★
단, refresh()를 꼭 해줘야함1 2 3 | ctx.load("classpath:applicationCTX.xml"); // 설정 ctx.refresh(); | cs |
3. 사용
평소 사용하던것 처럼
ctx.getBean을 사용한다.(참고 : https://cloud0477.tistory.com/59)
1 2 3 | Student student = ctx.getBean("student", Student.class); // 사용 System.out.println("이름 : " + student.getName()); System.out.println("나이 : " + student.getAge()); | cs |
4. 종료
사용하지 않을 경우 닫을수 있다.
1 | ctx.close(); // 종료 | cs |
----전체 예시-----
1 2 3 4 5 6 7 8 9 10 11 | GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); // 생성 ctx.load("classpath:applicationCTX.xml"); // 설정 ctx.refresh(); Student student = ctx.getBean("student", Student.class); // 사용 System.out.println("이름 : " + student.getName()); System.out.println("나이 : " + student.getAge()); ctx.close(); // 종료 | cs |
-Bean 생명주기
-기본적으로 컨테이너가 종료되면 자동적으로 Bean도 종료된다.
1. Bean 생성
refresh 할때 Bean 생성된다.
★메소드 실행순서★
refresh메소드 -> @PostConstruct(선행메서드) -> afterpropertiesSet() 메소드 실행!
|
1 2 3 4 5 6 7 8 9 10 | @PostConstruct //@(어노테이션)을 사용하면 자동으로 postConstruct로 인식함 public void initMethod() { System.out.println("initMethod()"); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("afterPropertiesSet()"); } | cs |
2. Bean 종료
close() 할때 종료된다.
★메소드 실행순서★
close메소드 -> @Predestroy(선행메서드) -> destroy() 메소드 실행!
※컨테이너말고 Bean만 종료시키고 싶으면 destroy() 메소드를 실행 시키면됨!
1 | ctx.close(); | cs |
1 2 3 4 5 6 7 8 9 10 | @PreDestroy //@(어노테이션)을 사용하면 자동으로 PreDestroy로 인식함 public void destroyMethod() { System.out.println("destroyMethod()"); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("destroy()"); } | cs |
-전체 소스
-main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:applicationCTX.xml"); System.out.println("load() 완료"); System.out.println("refresh() 전"); ctx.refresh(); System.out.println("refresh() 완료"); System.out.println("Close() 전"); ctx.close(); System.out.println("Close() 완료"); } | cs |
-xml
|
-A 클래스
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 | public class OtherStudent implements InitializingBean, DisposableBean{ @PostConstruct //인터페이스 불필요 public void initMethod() { //어노테이션만 쓰면됨 System.out.println("A_initMethod()"); } @Override public void afterPropertiesSet() throws Exception { //인터페이스필요 // TODO Auto-generated method stub System.out.println("A_afterPropertiesSet()"); } @PreDestroy //인터페이스 불필요 public void destroyMethod() { //어노테이션만 쓰면됨 System.out.println("A_destroyMethod()"); } @Override public void destroy() throws Exception { //인터페이스 필요 // TODO Auto-generated method stub System.out.println("A_destroy()"); } } | cs |
결과
-Bean 범위(Scope)
-Bean을 사용하여 객체를 생성하면, 그 객체는 기본설정으로 Singleton이다.
즉 새로생성하지 않고 하나의 객체로만 사용된다.
예를 들어보자
applicationCTX.xml
1 2 3 4 | <bean id="student" class="com.javalec.ex.Student" scope="singleton"> //scope=singleton으로 잡지않아도, 기본적으로 singleton으로 설정된다 <constructor-arg value="홍길순"></constructor-arg> <constructor-arg value="30"></constructor-arg> </bean> | cs |
main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml"); Student student1 = ctx.getBean("student", Student.class); Student student2 = ctx.getBean("student", Student.class); if(student1.equals(student2)) { <---------------------출력 결과 : student1 == student2 System.out.println("student1 == student2"); } else { System.out.println("student1 != student2"); } ctx.close(); | cs |
반응형
'프로그래밍 정리 > Spring' 카테고리의 다른 글
spring - Namespace 사용법 (0) | 2019.12.09 |
---|---|
Spring - Environment객체(Property값 가져오기) (0) | 2019.12.09 |
Spring - DI 2가지방법과 응용 (0) | 2019.12.04 |
spring - DI(Dependency Injection,의존 주입) (0) | 2019.12.04 |
Spring 설치 - 이클립스와 spring 연동 (0) | 2019.12.04 |