Bean

★Beanのライフサイクル

1.Beanの定義(設定ファイル)

<beans>

1.Singlton且つ非Lazyの場合、設定ファイルをロードする際にインスタンス化する

<bean id="sample" class="com.test.Sample" scope="singleton" />

<bean id="sample" class="com.test.Sample" singleton="true" />

2.Singlton且つLazyの場合、初めて使用する際にインスタンス化する

<bean id="sample" class="com.test.Sample" scope="singleton" lazy-init="true" />

3.非Singltonの場合、リクエストごとにインスタンス化する

<bean id="sample" class="com.test.Sample" scope="prototype" />

<bean id="sample" class="com.test.Sample" singleton="false" />

4.HTTPリクエストごとにインスタンス化する(有効範囲:該当HTTP Request)

<bean id="sample" class="com.test.Sample" scope="request" />

5.HTTPリクエストごとにインスタンス化する(有効範囲:該当HTTP Session)

<bean id="sample" class="com.test.Sample" scope="session" />

</beans>

※requestスコープとsessionスコープを使う前提

Servlet 2.4以上の場合

<listener>

<listener-class>

org.springframework.web.context.request.RequestContextListener

</listener-class>

</listener>

2.Beanの初期化

方法1

<bean id="sample" class="com.test.Sample" scope="singleton" init-method="init" />

public class Sample{

public void init(){

// do something

}

}

方法2 org.springframework.beans.factory.InitializingBean

public class Sample implement InitializingBean{

public void afterPropertiesSet() throws Exception{

// do something

}

}

3.Beanの呼び出し Singleton Pattern + Factory Pattern

方法1

XmlBeanFactory factory = new XmlBeanFactory(new FileInputStream("bean.xml"));

Sample sample = (Sample) factory.getBean("sample");

方法2

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");

//ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");

Sample sample1 = (Sample) ctx.getBean("sample");

Sample sample2 = (Sample) ctx.getBean("sample");

System.out.println( sample1 == sample2); // true

4.Beanの廃棄

方法1

<bean id="sample" class="com.test.Sample" scope="singleton" destroy-method="clear" />

public class Sample{

public void clear(){

// do something

}

}

方法2 org.springframework.beans.factory.DisposableBean

public class Sample implement DisposableBean{

public void destroy() throws Exception{

// do something

}

}

★Map Injection

書き方1

<bean id="emails" class="java.util.HashMap">

<constructor-arg>

<map>

<entry>

<key>

<value>Tom</value>

</key>

<value>tom@gmail.com</value>

</entry>

<entry>

<key>

<value>Jack</value>

</key>

<ref bean="mail" />

</entry>

</map>

<constructor-arg>

</bean>

書き方2

<bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean">

<property name="sourceMap">

<map>

<entry key="Tom" value="tom@gmail.com"/>

<entry key="Jack" value="jack@gmail.com"/>

</map>

</property>

</bean>

※ListFactoryBean、SetFactoryBeanも同様

書き方3

<util:map id="emails">

<entry key="Tom" value="tom@gmail.com"/>

<entry key="Jack" value="jack@gmail.com"/>

</util:map>

設定が必要

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.5.xsd">

★Bean Copy

速度:cglib > spring > apache

apache

import org.apache.commons.beanutils.BeanUtils;

BeanUtils.copyProperties(targetBean, sourceBean);

cglib

import net.sf.cglib.beans.BeanCopier;

private BeanCopier beanCopier = BeanCopier.create(SourceBean.class, TargetBean.class, false);

beanCopier.copy(sourceBean, targetBean, null);

spring

import org.springframework.beans.BeanUtils;

BeanUtils.copyProperties(sourceBean, targetBean);