[color=red][b]bean的生命周期(初始化和销毁)[/b][/color][color=red]EmpService.java实体类[/color]
/*默认无参数的构造器*/
public EmpService() {
super();
System.out.println(".......EmpService实例被创建......");
}
/*初始化的方法*/
public void init(){
System.out.println(".....EmpService被初始化的方法.....");
}
/*销毁的方法*/
public void destroy(){
System.out.println(".....EmpSerivce被销毁的方法.....");
}
[color=red]App.java测试类[/color]
package cn.csdn.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@Test
public void App(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:a*.xml");
//关闭
ClassPathXmlApplicationContext acc=(ClassPathXmlApplicationContext) ac;
acc.close();
}
}
[color=red]applicationContext.xml配置文件[/color]
<bean id="empService" class="cn.csdn.service.EmpService" scope="singleton" init-method="init" destroy-method="destroy"/>
/*默认无参数的构造器*/
public EmpService() {
super();
System.out.println(".......EmpService实例被创建......");
}
/*初始化的方法*/
public void init(){
System.out.println(".....EmpService被初始化的方法.....");
}
/*销毁的方法*/
public void destroy(){
System.out.println(".....EmpSerivce被销毁的方法.....");
}
[color=red]App.java测试类[/color]
package cn.csdn.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@Test
public void App(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:a*.xml");
//关闭
ClassPathXmlApplicationContext acc=(ClassPathXmlApplicationContext) ac;
acc.close();
}
}
[color=red]applicationContext.xml配置文件[/color]
<bean id="empService" class="cn.csdn.service.EmpService" scope="singleton" init-method="init" destroy-method="destroy"/>