Spring 2.5が管理するBeanのライフサイクル
Spring 2.5 Benのライフサイクル
初期化
init-method=「beanクラス定義の初期方法を指定する」
廃棄する
destroy-method=「指定されたbean類定義の廃棄方法」
beanの構成例:
beanの実装はがデフォルトの一例であれば、Spring容器起動時に実装され、
scopeが単一の例でない場合、このbeanを呼び出した時に初めて化されます.scope="singleton"が欲しい時に呼び出しを実行します.遅延の実例化属性lazy-nit="true"falseを設定することができます.
--------------------------------
コードのサンプル:
初期化
init-method=「beanクラス定義の初期方法を指定する」
廃棄する
destroy-method=「指定されたbean類定義の廃棄方法」
beanの構成例:
beanの実装は
scopeが単一の例でない場合、このbeanを呼び出した時に初めて化されます.scope="singleton"が欲しい時に呼び出しを実行します.遅延の実例化属性lazy-nit="true"falseを設定することができます.
--------------------------------
コードのサンプル:
package test.service;
public interface PersonService {
public void save();
}
package test.service.impl;
import test.service.PersonService;
public class PersonServiceBean implements PersonService {
public PersonServiceBean(){
System.out.println(" ");
}
public void init(){
System.out.println(this.getClass().getName() + " ");
}
public void save(){
System.out.println("sava function");
}
public void destroy(){
System.out.println(this.getClass().getName() + " ");
}
}
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.service.PersonService;
//
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception{
}
@Test public void instanceSpring(){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService p = (PersonService) ctx.getBean("personService");//get bean
p.save();//
ctx.close();// spring , destroy
}
}
//
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
<bean id="personService" class="test.service.impl.PersonServiceBean" scope="singleton" lazy-init="true" init-method="init" destroy-method="destroy"></bean>
</beans>
// scope="singleton" bean , scope
// lazy-init="true" getBean bean
// init-method="init" init() bean
// destroy-method="destroy" destroy bean, AbstractApplicationContext close , ,