xml 파일에 bean태그를 이용하여 bean을 직접 정의할 수 있다.

bean태그에는 bean객체의 생성시점과 생명주기를 직접 설정할 수 있는 속성들이 있다 한개한개 알아보자.

 

 

 

lazy-init

 

게으른..초기화..? 

이 속성을 true값으로 설정하면 xml을 로딩할 때 객체가 바로 생성되지 않고 getBean을 사용해야 객체가 생성된다.

 

 

 

<bean id="t1" class="com.study.spring.beans.TestBean" lazy-init="true"/>

 

 

// xml이 로딩됨. lazy-init속성이 true이면 이때 객체가 생성되지 않는다.
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/study/spring/config/beans.xml");
		
// getBean을 사용하여 bean을 블러옴. lazy-init속성이 true이면 이때 객체가 생성된다.
TestBean t1 = ctx.getBean("t1", TestBean.class);

 

 

 

 

 

 

 

scope

 

scope속성이 singleton이면 한번 생성된 객체가 다시 생성되지 않는다. (singleton이 default값이다.)

scope속성이 prototype이면 호출 될 때마다 객체를 생성할 수 있다.

 

 

 

<bean id="t1" class="com.study.spring.beans.TestBean" lazy-init="true" scope="prototype" />

 

 

 

lazy-init이 true이므로 getBean을 호출해야 객체가 생성되며, scope가 prototype이므로 getBean을 호출 할 때마다 다른 객체가 생성된다.

 

 

 

// xml이 로딩됨. lazy-init속성이 true이기 때문에 이때 객체가 생성되지 않는다.
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/study/spring/config/beans.xml");
		
// getBean을 사용하여 bean을 블러옴. lazy-init속성이 true이기 때문에 이때 객체가 생성된다.
TestBean t1 = ctx.getBean("t1", TestBean.class);

// getBean을 이용하여 동일한 bean인 "t1"을 호출하였지만 t1과 t2는 서로 다른 객체이다.
TestBean t2 = ctx.getBean("t1", TestBean.class);

 

 

 

 

 

 

 

init-method 와 destroy-method

 

init-method는 객체가 생성될 때 호출되는 메소드를 정의하는 속성이다.

destroy-method는 객체가 소멸될 때 호출되는 메소드이다. (ctx.close(); 하면 IoC컨테이너 내에 존재하던 모든 객체는 소멸된다.)

 

 

 

<bean id="t1" class="com.study.spring.beans.TestBean" lazy-init="true" init-method="beanInit" destroy-method="beanDestroy" />

 

 

 

 

TestBean 클래스에 beanInit 메소드와 beanDestroy 메소드가 사용자에 의해 각각 "init메소드", "destroy메소드" 라고 출력하도록 정의되었다고 가정한다. 위와 같이 bean을 정의 하였다면,

 

 

 

 

 

// xml이 로딩됨. lazy-init속성이 true이기 때문에 이때 객체가 생성되지 않는다.
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/study/spring/config/beans.xml");
		
// getBean을 사용하여 bean을 블러옴. lazy-init속성이 true이기 때문에 이때 객체가 생성된다.
// init-method가 정의되어 있으므로 이곳에서 호출된다.

TestBean t1 = ctx.getBean("t1", TestBean.class);
System.out.printf("%s :  t1객체입니다.\n", t1);

// destroy-method가 정의되어 있으므로 이곳에서 호출된다.

 

 

 

 

출력결과는

 

init메소드

com.study.spring.beans.TestBean@12345 :  t1객체입니다.

destroy메소드

 

이렇게 될것이다!!

 

 

 

 

 

 

 

default-init-method 와 default-destroy-method

 

bean태그에 속성으로 정의했던 init-method와 destroy-method와는 달리 상위태그인 beans태그의 속성으로 지정된다.

만약 bean태그에 속성으로 init-method나 destroy-method가 정의되어 있지 않다면 default-init-method 나 default-destroy-method가 호출이 된다.

 

만약에 default-init-method 와 default-destroy-method, init-method와 destroy-method가 모두 정의되어 있다면????

default는 무시되고 bean에 지정되어 있는 메소드가 호출된다.

 

 

 

 

default-init-method 와 default-destroy-method 속성이 사용되고 있으나, 속성값으로 정의된 메소드가 정의되어 있지 않다면 .. 오류가 나지 않는다. 

하지만, init-method와 destroy-method는 속성이 사용되고 있으나, 속성값으로 정의된 메소드가 정의되어 있지 않다면???? 오류난다.

 

즉, 메소드가 없을 경우

default-init-method 와 default-destroy-method -> 아무일도 일어나지 않음

init-method와 destroy-method -> 오류남.

 

 

 

 

 

 

 

 

BeanPostProcessor

 

BeanPostProcessor는 인터페이스이며 postProcessBeforeInitialization, postProcessAfterInitialization 라는 메소드를 정의하고있다.

postProcessBeforeInitialization는 init메소드가 호출되기 전에 먼저 호출되는 함수이다.

postProcessAfterInitialization는 intit메소드가 호출 된 후에 호출되는 함수이다. 

즉, init메소드 호출 전 후로 가로채기 하는 메소드이다!

 

 

 

//init 메서드 호출 전
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	// TODO Auto-generated method stub
	System.out.println("before");
	return bean;
}

	
//init 메서드 호출 후
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	// TODO Auto-generated method stub
	System.out.println("after");
	return bean;
}

 

 

구현 클래스이기 때문에 Override를 해준것이고, 반환값은 Object 형인 bean이다. 

bean객체마다 다음 메소드들을 활용하려면 String형 beanName매개변수를 사용하여 bean객체의 id값을 이용하여 함수를 활용하면 되겠다.

'JAVA > Spring' 카테고리의 다른 글

컬렉션 주입  (0) 2020.04.09
의존성 주입 (Dependency Injection)  (0) 2020.04.09
IoC 컨테이너  (0) 2020.04.07
Maven 설정  (0) 2020.04.07
스프링 MVC 프레임워크의 구조  (0) 2020.03.31

+ Recent posts