package Cuiyw.Spring.Service;
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.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class LifeCycle implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware{
private String name;
public String getName() {
System.out.println("getName name="+name);
return name;
}
public void setName(String name) {
System.out.println("setName name="+name);
this.name = name;
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("InitializingBean.afterPropertiesSet()");
}
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("BeanNameAware.setBeanName");
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanFactoryAware.setBeanFactory");
}
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("DisposableBean.destroy");
}
public void myInit() {
System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
}
public void myDestory() {
System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("ApplicationContextAware.setApplicationContext");
}
}
package Cuiyw.Spring.Service;
import java.beans.PropertyDescriptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor{
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization");
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization");
return bean;
}
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
return true;
}
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
return null;
}
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");
return pvs;
}
}
package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanPostProcessor.postProcessAfterInitialization ");
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanPostProcessor.postProcessBeforeInitialization");
return bean;
}
}
package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory");
}
}
<?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="beanPostProcessor" class="Cuiyw.Spring.Service.MyBeanPostProcessor"></bean> <bean id="instantiationAwareBeanPostProcessor" class="Cuiyw.Spring.Service.MyInstantiationAwareBeanPostProcessor"></bean> <bean id="beanFactoryPostProcessor" class="Cuiyw.Spring.Service.MyBeanFactoryPostProcessor"></bean> <bean id="lifeCycle" class="Cuiyw.Spring.Service.LifeCycle" init-method="myInit" destroy-method="myDestory"> <property name="name" value="cuiyw1"></property> </bean> </beans>
package Cuiyw.SpringAop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import Cuiyw.Spring.IService.IService;
import Cuiyw.Spring.Service.LifeCycle;
public class App
{
public static void main( String[] args )
{
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
BeanFactory factory=context;
LifeCycle lifeCycle=factory.getBean("lifeCycle",LifeCycle.class);
lifeCycle.setName("cuiyw2");
System.out.println("lifeCycle.name="+lifeCycle.getName());
((ClassPathXmlApplicationContext)factory).registerShutdownHook();
/*service=(IService)factory.getBean("ServiceA");
service.service("Cuiyw ServiceA");
service=(IService)factory.getBean("ServiceImpl");
service.service("Cuiyw ServiceImpl"); */
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有