<?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.xsd"> <!-- 需要手动指定初始化方法和销毁方法--> <bean id="vip" class="com.powernode.spring6.bean.Vip"></bean> <bean id="user" class="com.powernode.spring6.bean.User" init-method="initBean" destroy-method="destroyBean"> <property name="name" value="Jack"/> </bean> <!-- 第一步实例化,第二步赋值,第三步初始化,第四步使用,第五步销毁,七步就是在第三步初始化前后添加代码--> <!-- Bean生命周期七步,第一步实例化,第二步赋值,第三步Bean后处理器的before方法,第四步初始化,第五步Bean后处理器的after方法,第六步使用,第七步销毁Bean--> <!-- 配置Bean后处理器,该处理器将作用于整个配置文件所有的Bean--> <bean class="com.powernode.spring6.bean.LogBeanPostProcessor"></bean> </beans>
<?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.xsd">
<!-- 需要手动指定初始化方法和销毁方法-->
<bean id="vip" class="com.powernode.spring6.bean.Vip"></bean>
<bean id="user" class="com.powernode.spring6.bean.User" init-method="initBean" destroy-method="destroyBean">
<property name="name" value="Jack"/>
</bean>
<!-- 第一步实例化,第二步赋值,第三步初始化,第四步使用,第五步销毁,七步就是在第三步初始化前后添加代码-->
<!-- Bean生命周期七步,第一步实例化,第二步赋值,第三步Bean后处理器的before方法,第四步初始化,第五步Bean后处理器的after方法,第六步使用,第七步销毁Bean-->
<!-- 配置Bean后处理器,该处理器将作用于整个配置文件所有的Bean-->
<bean class="com.powernode.spring6.bean.LogBeanPostProcessor"></bean>
</beans>
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class LogBeanPostProcessor implements BeanPostProcessor { private static final Logger logger = LoggerFactory.getLogger(LogBeanPostProcessor.class); @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { logger.info("执行了Bean后处理器的Before"); return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName); } //方法有两个参数,一个是刚刚创建的Bean对象,一个是Bean的名字 @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { logger.info("执行了Bean后处理器的After"); return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); } //日志Bean后处理器,在初始化前和之后都记录日志 }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class LogBeanPostProcessor implements BeanPostProcessor
{
private static final Logger logger = LoggerFactory.getLogger(LogBeanPostProcessor.class);
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
logger.info("执行了Bean后处理器的Before");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
//方法有两个参数,一个是刚刚创建的Bean对象,一个是Bean的名字
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
logger.info("执行了Bean后处理器的After");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
//日志Bean后处理器,在初始化前和之后都记录日志
}
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class User { private static final Logger logger = LoggerFactory.getLogger(User.class); private String name; public User() { logger.info("User的无参数构造方法,1"); } //第一步实例化Bean,给Bean属性赋值(通过set方法) //初始化Bean(调用Bean的init方法,需要自己写自己配),使用Bean,销毁Bean(调用destroy方法,需要自己写自己配) public void setName(String name) { this.name = name; logger.info("给属性赋值,2"); } public void initBean() { //需要自己写,自己配 logger.info("初始化Bean,3"); } public void destroyBean() { //需要自己写,自己配 logger.info("销毁Bean,5"); } }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class User
{
private static final Logger logger = LoggerFactory.getLogger(User.class);
private String name;
public User()
{
logger.info("User的无参数构造方法,1");
}
//第一步实例化Bean,给Bean属性赋值(通过set方法)
//初始化Bean(调用Bean的init方法,需要自己写自己配),使用Bean,销毁Bean(调用destroy方法,需要自己写自己配)
public void setName(String name)
{
this.name = name;
logger.info("给属性赋值,2");
}
public void initBean()
{
//需要自己写,自己配
logger.info("初始化Bean,3");
}
public void destroyBean()
{
//需要自己写,自己配
logger.info("销毁Bean,5");
}
}
package com.powernode.spring6.bean; public class Vip { }
package com.powernode.spring6.bean;
public class Vip {
}
package com.powernode.spring6.test; import com.powernode.spring6.bean.User; import com.powernode.spring6.bean.Vip; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { private static final Logger logger = LoggerFactory.getLogger(Test.class); @org.junit.Test public void TestLifeCycleFive() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); User user = applicationContext.getBean("user",User.class); logger.info("使用Bean,4"); ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext; context.getBean("vip", Vip.class); context.close(); } }
package com.powernode.spring6.test;
import com.powernode.spring6.bean.User;
import com.powernode.spring6.bean.Vip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
private static final Logger logger = LoggerFactory.getLogger(Test.class);
@org.junit.Test
public void TestLifeCycleFive()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = applicationContext.getBean("user",User.class);
logger.info("使用Bean,4");
ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;
context.getBean("vip", Vip.class);
context.close();
}
}