Spring/Spring Quick Start

스프링퀵스타트 bean 엘리먼트 속성

ITGenerations 2019. 5. 21. 06:52

1. init-method 속성

서블릿은 init() 메소드를 재정의하여 멤버변수로 초기화한다.

 

2. destroy-mehtod 속성

스프링 컨테이너가 객체를 삭제하기 직전에 호출될 임의의 메소드를 지정할 수 있다.

 

3. lazy-init 속성

스프링에서는 컨테이너가 구동되는 시점이 아닌 해당 <bena>이 사용되는 시점에 객체를 생성하도록 init-lazy 속성을 제공한다. 이를 이용하면 메모리 관리를 효율적으로 할 수 있다.

 

ApplicationContext를 이용하여 컨테이너를 구동하면 컨테이너가 구동되는 시점에 스프링 설정 파일에 등록된 <bean>들을 생성하는 즉시 로딩(pre-loading)방식으로 동작된다. 그런데 어떤 <bean>은 자주 사용되지 않으면서 메모리를 많이 차지하여 시스템에 부담을 주는 경우도 있다. 이럴때 lazy-init을 사용하여 메모리 관리를 해주면 좋다.

 

4. scope 속성

스프링 컨테이너는 컨테이너가 생성한 bean을 어느 범위에서 사용할 수 있는지를 지정할 수 있는데, 이 때 scope속성을 사용한다. 

 

scope 기본 속성값은 싱글톤이다. 이는 해당 bean이 스프링 컨테이너에 의해 단 하나만 생성되어 운용되도록한다.

 

종류는 singlton/request/session/prototype 이 있다.

상세한 설명은 아래 내용 참조

Attribute : scope
The scope of this bean: typically "singleton" (one shared instance, which will be returned by all calls to 
 getBean with the given id), or "prototype" (independent instance resulting from each call to getBean). By 
 default, a bean will be a singleton, unless the bean has a parent bean definition in which case it will inherit 
 the parent's scope. Singletons are most commonly used, and are ideal for multi-threaded service objects. 
 Further scopes, such as "request" or "session", might be supported by extended bean factories (e.g. in a web 
 environment). Inner bean definitions inherit the scope of their containing bean definition, unless explicitly 
 specified: The inner bean will be a singleton if the containing bean is a singleton, and a prototype if the 
 containing bean is a prototype, etc.

Data Type : string

 

 

 

 

 

 

-SamsungTV.java-

package polymorphism;

public class SamsungTV implements TV{
public void initMethod() {
System.out.println("객체 초기화 작업 처리...");
}
public void destroyMethod() {
System.out.println("객체 삭제 전에 처리할 로직 처리...");
}
public void powerOn() {
System.out.println("samsungTV on");
}
public void powerOff() {
System.out.println("samsungTV off");
}
public void volumeUp() {
System.out.println("samsungTV volumeUp");
}
public void volumeDown() {
System.out.println("samsungTV volumeDown");
}


}

 

 

-applicationContext.xml 일부분-

<bean id="tv" class="polymorphism.SamsungTV" init-method="initMethod" />

<bean class="polymorphism.SamsungTV" destroy-method="destroyMethod" />

 

동일한 class를 가졌지만 id와 name은 유일해야하므로 1개만 사용한다.

 

 

-TVUser.java-

package polymorphism;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class TVUser {

public static void main(String[] args) {
// 1. Spring 컨테이너를 구동한다.
AbstractApplicationContext factory = 
new GenericXmlApplicationContext("applicationContext.xml");

// 2. Spring 컨테이너로부터 필요한 객체를 요청(look up)한다.
TV tv = (TV)factory.getBean("tv");
tv.powerOn();
tv.volumeUp();
tv.volumeDown();
tv.powerOff();

// 3. Spring 컨테이너를 종료한다.
factory.close();
}

}

 

NFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@1ee0005: startup date [Tue May 21 06:50:36 KST 2019]; root of context hierarchy
객체 초기화 작업 처리...
samsungTV on
samsungTV volumeUp
samsungTV volumeDown
samsungTV off
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@1ee0005: startup date [Tue May 21 06:50:36 KST 2019]; root of context hierarchy
객체 삭제 전에 처리할 로직 처리...