标签:spring
一:基本流程
把一个bean纳入到Spring IoC容器之中,这个bean的生命周期就会交由容器进行管理
1.Bean的建立在容器关闭时,可以在Bean定义文件中使用“destory-method”定义的方法
二:使用案例
1.案例截图
2.基本代码
package com.cloud.beanlife;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean{
private String name;
private Integer age;
public PersonService(){
System.out.println("实例化对象");
}
public PersonService(String abc){
System.out.println("实例化对象");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("调用了setName函数");
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void sayhello(){
System.out.println("hello:"+name+"的年龄是"+age);
}
//该方法可以传递ApplicationContext
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("setApplicationContext "+arg0);
}
//可以通过注解的方式配置<bean init-method="init">初始化方法
@PostConstruct
public void init(){
System.out.println("自己的init方法");
}
//该方法可以传递beanFactory
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("setBeanFactory "+arg0);
}
//该方法的arg0表示正在被实例化的bean的id 是多少
@Override
public void setBeanName(String arg0) {
System.out.println("setBeanName被调用 值是"+arg0);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
//可以通过注解的方式配置<bean destroy-method="mydestory">销毁方法
@PreDestroy
public void mydestroy(){
System.out.println("生命周期结束,释放各种资源");
}
}
package com.cloud.beanlife;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessAfterInitialization 函数被调用");
System.out.println(arg0+"被创建的 时间是"+new java.util.Date());
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessBeforeInitialization 函数被调用");
return arg0;
}
}
3.配置代码
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="personService" destroy-method="mydestroy" class="com.cloud.beanlife.PersonService">
<property name="name" value="Spring"/>
<property name="age">
<value>25</value>
</property>
</bean>
<bean id="personService2" class="com.cloud.beanlife.PersonService">
<property name="name" value="小明"/>
</bean>
<!-- 配置自己的处理器,类似于过滤器 -->
<bean id="myBeanPostProcessor" class="com.cloud.beanlife.MyBeanPostProcessor">
</bean>
</beans>
4.测试代码
package com.cloud.beanlife;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestLife {
public static void main(String[] args) {
//bena的生命周期
ApplicationContext ac=new ClassPathXmlApplicationContext("com/cloud/beanlife/beans.xml");
PersonService ps=(PersonService) ac.getBean("personService");
ps.sayhello();
}
}
5.测试结果
实例化对象
调用了setName函数
setBeanName被调用 值是personService
setBeanFactory org.springframework.beans.factory.support.DefaultListableBeanFactory@9a082e2: defining beans [personService,personService2,myBeanPostProcessor]; root of factory hierarchy
setApplicationContext org.springframework.context.support.ClassPathXmlApplicationContext@735cda3f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@735cda3f]; startup date [Thu Aug 27 16:43:00 CST 2015]; root of context hierarchy
postProcessBeforeInitialization 函数被调用
afterPropertiesSet
postProcessAfterInitialization 函数被调用
com.cloud.beanlife.PersonService@6a5f6303被创建的 时间是Thu Aug 27 16:43:01 CST 2015
实例化对象
调用了setName函数
setBeanName被调用 值是personService2
setBeanFactory org.springframework.beans.factory.support.DefaultListableBeanFactory@9a082e2: defining beans [personService,personService2,myBeanPostProcessor]; root of factory hierarchy
setApplicationContext org.springframework.context.support.ClassPathXmlApplicationContext@735cda3f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@735cda3f]; startup date [Thu Aug 27 16:43:00 CST 2015]; root of context hierarchy
postProcessBeforeInitialization 函数被调用
afterPropertiesSet
postProcessAfterInitialization 函数被调用
com.cloud.beanlife.PersonService@42bad8a8被创建的 时间是Thu Aug 27 16:43:01 CST 2015
hello:Spring的年龄是25
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:spring
原文地址:http://blog.csdn.net/dzy21/article/details/48031531