| singleton | [기본 설정] Spring IoC Container 내에서 Bean 정의 당 하나의 Bean 객체 생성 |
| prototype | 매번 같은 Type의 새로운 Bean 객체 생성 |
| request | WebApplicationContext 유형의 Container 사용 시, Http request 당 하나의 Bean 객체 생성 |
| session | WebApplicationContext 유형의 Container 사용 시, Http session 당 하나의 Bean 객체 생성 |
| globalSession | WebApplicationContext 유형의 Container 사용 시, portlet context 내에서만 유효하며 global Http session 당 하나의 Bean 객체 생성 |

<bean id="userService" class="com.sds.emp.user.services.impl.UserServiceImpl" scope="singleton”>
<property name="userDAO" ref="userDAO" />
</bean>
<bean id="userDAO" class="com.sds.emp.user.services.impl.UserDAO”>
중략…
</bean>

<bean id="userService" class="com.sds.emp.user.services.impl.UserServiceImpl" scope="prototype”>
<property name="userDAO" ref="userDAO" />
</bean>
<!-- a HTTP Session-scoped bean exposed as a proxy -->
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy/>
</bean>
<!-- a singleton-scoped bean injected with a proxy to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
<!-- a reference to the proxied 'userPreferences' bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
<!-- 신규 Scope 정의를 위한 클래스를 정의하고,
org.springframework.beans.factory.config.Scope 인터페이스를 implement한다.-->
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<!-- CustomScopeConfigurer를 이용하여 Custom Scope 등록 -->
<property name="scopes">
<map>
<entry key="thread">
<bean class="com.foo.ThreadScope"/>
</entry>
</map>
</property>
</bean>
<!-- Custom Scope 사용 -->
<bean id="bar" class="x.y.Bar" scope="thread">
<property name="name" value="Rick"/>
<aop:scoped-proxy/>
</bean>


public class IoCServiceImpl1 implements IoCService1, ApplicationContextAware {
public void setApplicationContext (ApplicationContext context){
IoCService2 iocService2 = (IoCService2)context.getBean("IoCService2");
}
}
public class IoCServiceImpl1 implements IoCService1, MessageSourceAware {
private MessageSource messageSource;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
}
<bean id="com.sds.emp.user.services.UserServiceWithIoCExtended" class="com.sds.emp.user.services.impl.UserServiceImplWithIoCExtended" init-method="userInitialize" destroy-method="userDestroy" parent="parent"> </bean>

<bean id="com.sds.emp.user.services.UserServiceWithIoCExtended" class="com.sds.emp.user.services.impl.UserServiceImplWithIoCExtended" init-method="userInitialize" destroy-method="userDestroy" parent="parent"> </bean>
<!-- register parent bean that has a dependency with userDAO bean --> <bean id="parent" abstract="true"> <property name="userDAO" ref="userDAO" /> </bean> <bean id="com.sds.emp.user.services.UserServiceWithIoCExtended" class="com.sds.emp.user.services.impl.UserServiceImplWithIoCExtended" init-method="userInitialize" destroy-method="userDestroy" parent="parent"> </bean>
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
// simply return the instantiated bean as-is
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean; // we could potentially return any object reference here...
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("Bean '" + beanName + "' created : " + bean.toString());
return bean;
}
}
<bean class="scripting.InstantiationTracingBeanPostProcessor"/>
public class BeanCounterBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
throws BeansException {
…중략
}
<bean class="test.BeanCounterBeanFactoryPostProcessor"/>
<!-- set file locations -->
<bean id="configurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>userConfigurer.txt</value>
</list>
</property>
</bean>
<bean id="com.sds.emp.user.services.UserServiceWithIoCExtended"
class="com.sds.emp.user.services.impl.UserServiceImplWithIoCExtended">
<property name="userDAO" ref="userDAO" />
<!-- set userCompany value using key name in properties file -->
<property name="userCompany" value="${user.company}"></property>
</bean>
user.company=SamsungSDS
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.springinaction.knight.PhoneNumber"> <bean id="phoneEditor" class="com.springinaction.springcleaning.PhoneNumberEditor" /> </entry> </map> </property> </bean>
<bean id="knight" class="com.springinaction.knight.KnightOnCall"> <property name="url" value="http://www.knightoncall.com" /> <property name="phoneNumber" value="940-555-1234" /> </bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list><value>userMessages</value></list> </property> </bean>
argument.required=The "{0}" argument is required.
new String(messageSource.getMessage("argument.required", new Object[] {"userVO"}, Locale.KOREA)
.getBytes("8859_1"), "euc-kr")
"userVO" 인자는 반드시 필요하다.
| ContextRefreshedEvent | ApplicationContext가 초기화되거나 갱신(refresh)될 때 발생하는 이벤트 - 여기서 초기화는 모든 Bean이 로드되고 Singleton Bean들은 미리 인스턴스화되며 ApplicationContext는 사용할 준비가 된다는 것을 의미함 |
| ContextClosedEvent | ApplicationContext의 close()메소드를 사용하여 ApplicationContext가 종료될 때 발생하는 이벤트 - 여기서 종료는 Singleton Bean들이 소멸(destroy)되는 것을 의미함 |
| RequestHandledEvent | HTTP Request가 처리되었을 때 WebApplicationContext 내에서 발생하는 이벤트 - 이 이벤트는 Spring의 DispatcherServlet을 사용하는 웹 어플리케이션에서만 적용 가능함 |
public class RefreshListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent evt) {
if (evt instanceof ContextRefreshedEvent) {
// 중략 …
}
}
}
<bean id="refreshListener" class="sample.RefreshListener"/>
<bean id="userEventListener" class="com.sds.emp.user.services.impl.UserEventListener"/>
public class UserEventListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent evt) {
if (evt instanceof UserEvent) {
UserEvent event = (UserEvent)evt;
System.out.println("Received in UserEventListener : " + event.getUserMessage());
}
}
}
this.ctx.publishEvent(new UserEvent(this,"new user is added successfully."));
| Bean instantiation/wiring | ||
| Automatic BeanPostProcessor registration | ||
| Automatic BeanFactoryPostProcessor registration | ||
| Convenient MessageSource access (for i18n) | ||
| ApplicationEvent publication |