/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public class LifecycleBean implements InitializingBean, DisposableBean
{
@SuppressWarnings("unused")
private String lifeCycleBeanName;
public void setLifeCycleBeanName(String lifeCycleBeanName)
{
System.out.println("Enter LifecycleBean.setLifeCycleBeanName(), lifeCycleBeanName = " + lifeCycleBeanName);
this.lifeCycleBeanName = lifeCycleBeanName;
}
public void destroy() throws Exception
{
System.out.println("Enter LifecycleBean.destroy()");
}
public void afterPropertiesSet() throws Exception
{
System.out.println("Enter LifecycleBean.afterPropertiesSet()");
}
public void beanStart()
{
System.out.println("Enter LifecycleBean.beanStart()");
}
public void beanEnd()
{
System.out.println("Enter LifecycleBean.beanEnd()");
}
}
<?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-4.1.xsd">
<bean id="lifeCycleBean" class="org.xrq.bean.lifecycle.LifecycleBean">
<property name="lifeCycleBeanName" value="lifeCycleBean" />
</bean>
</beans>
Enter LifecycleBean.setLifeCycleBeanName(), lifeCycleBeanName = lifeCycleBean Enter LifecycleBean.afterPropertiesSet() Enter LifecycleBean.beanStart() Enter LifecycleBean.destroy() Enter LifecycleBean.beanEnd()
/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public class AwareBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware
{
private String beanName;
private ApplicationContext applicationContext;
private BeanFactory beanFactory;
public void setBeanName(String beanName)
{
System.out.println("Enter AwareBean.setBeanName(), beanName = " + beanName + "\n");
this.beanName = beanName;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
System.out.println("Enter AwareBean.setApplicationContext(), applicationContext = " + applicationContext + "\n");
this.applicationContext = applicationContext;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException
{
System.out.println("Enter AwareBean.setBeanFactory(), beanfactory = " + beanFactory + "\n");
this.beanFactory = beanFactory;
}
}
<?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-4.1.xsd"> <bean id="AwareBean" class="org.xrq.bean.aware.AwareBean" /> </beans>
Enter AwareBean.setBeanName(), beanName = AwareBean Enter AwareBean.setBeanFactory(), beanfactory = org.springframework.beans.factory.support.DefaultListableBeanFactory@2747fda0: defining beans [AwareBean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy Enter AwareBean.setApplicationContext(), applicationContext = org.springframework.context.support.GenericApplicationContext@5514cd80: startup date [Mon Aug 08 19:23:30 CST 2016]; root of context hierarchy
public interface Animal
{
public void move();
}
public class Monkey implements Animal
{
public void move()
{
System.out.println("Monkey move!");
}
}
public class Tiger implements Animal
{
public void move()
{
System.out.println("Tiger move!");
}
}
/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public class AnimalFactoryBean implements FactoryBean<Animal>
{
private String animal;
public Animal getObject() throws Exception
{
if ("Monkey".equals(animal))
{
return new Monkey();
}
else if ("Tiger".equals(animal))
{
return new Tiger();
}
else
{
return null;
}
}
public Class<?> getObjectType()
{
return Animal.class;
}
public boolean isSingleton()
{
return true;
}
public void setAnimal(String animal)
{
this.animal = animal;
}
}
<?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-4.1.xsd">
<bean id="animal" class="org.xrq.bean.factory.AnimalFactoryBean">
<property name="animal" value="Tiger"/>
</bean>
</beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:spring.xml",
})
public class BaseTest
{
@Resource
private Animal animal;
@Test
public void aa()
{
animal.move();
}
}
public class CommonBean
{
private String commonName;
public void setCommonName(String commonName)
{
this.commonName = commonName;
}
public void initMethod()
{
System.out.println("Enter CommonBean.initMethod(), commonName = " + commonName);
}
}
/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public class PostProcessorBean implements BeanPostProcessor
{
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
{
System.out.println("Enter ProcessorBean.postProcessAfterInitialization()\n");
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
{
System.out.println("Enter ProcessorBean.postProcessBeforeInitialization()");
return bean;
}
}
<?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-4.1.xsd">
<bean id="common0" class="org.xrq.bean.common.CommonBean" init-method="initMethod">
<property name="commonName" value="common0"/>
</bean>
<bean id="common1" class="org.xrq.bean.common.CommonBean" init-method="initMethod">
<property name="commonName" value="common1"/>
</bean>
<bean id="postProcessorBean" class="org.xrq.bean.processor.PostProcessorBean" />
</beans>
Enter ProcessorBean.postProcessBeforeInitialization() Enter CommonBean.initMethod(), commonName = common0 Enter ProcessorBean.postProcessAfterInitialization() Enter ProcessorBean.postProcessBeforeInitialization() Enter CommonBean.initMethod(), commonName = common1 Enter ProcessorBean.postProcessAfterInitialization() Enter ProcessorBean.postProcessBeforeInitialization() Enter ProcessorBean.postProcessAfterInitialization()
/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public class FactoryPostProcessorBean implements BeanFactoryPostProcessor
{
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurablelistablebeanfactory)
throws BeansException
{
System.out.println("Enter FactoryPostProcessorBean.postProcessBeanFactory()\n");
}
}
Enter FactoryPostProcessorBean.postProcessBeanFactory() Enter ProcessorBean.postProcessBeforeInitialization() Enter CommonBean.initMethod(), commonName = common0 Enter ProcessorBean.postProcessAfterInitialization() Enter ProcessorBean.postProcessBeforeInitialization() Enter CommonBean.initMethod(), commonName = common1 Enter ProcessorBean.postProcessAfterInitialization() Enter ProcessorBean.postProcessBeforeInitialization() Enter ProcessorBean.postProcessAfterInitialization()
/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurablelistablebeanfactory)
throws BeansException
{
BeanDefinition beanDefinition = configurablelistablebeanfactory.getBeanDefinition("common0");
MutablePropertyValues beanProperty = beanDefinition.getPropertyValues();
System.out.println("scope before change:" + beanDefinition.getScope());
beanDefinition.setScope("singleton");
System.out.println("scope after change:" + beanDefinition.getScope());
System.out.println("beanProperty:" + beanProperty);
}
scope before change: scope after change:singleton beanProperty:PropertyValues: length=1; bean property 'commonName'
public class CommonBean
{
public CommonBean()
{
System.out.println("Enter CommonBean's constructor");
}
private String commonName;
public void setCommonName(String commonName)
{
System.out.println("Enter CommonBean.setCommonName(), commonName = " + commonName);
this.commonName = commonName;
}
public void initMethod()
{
System.out.println("Enter CommonBean.initMethod(), commonName = " + commonName);
}
}
/**
* @author 五月的仓颉 http://www.cnblogs.com/xrq730/p/5721366.html
*/
public class InstantiationAwareBeanPostProcessorBean implements InstantiationAwareBeanPostProcessor
{
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
{
System.out.println("Enter InstantiationAwareBeanPostProcessorBean.postProcessAfterInitialization()");
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
{
System.out.println("Enter InstantiationAwareBeanPostProcessorBean.postProcessBeforeInitialization()");
return bean;
}
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
{
System.out.println("Enter InstantiationAwareBeanPostProcessorBean.postProcessAfterInstantiation()");
return true;
}
public Object postProcessBeforeInstantiation(Class<?> bean, String beanName) throws BeansException
{
System.out.println("Enter InstantiationAwareBeanPostProcessorBean.postProcessBeforeInstantiation()");
return null;
}
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pd, Object bean,
String beanName) throws BeansException
{
return pvs;
}
}
<?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-4.1.xsd">
<bean id="common" class="org.xrq.bean.common.CommonBean">
<property name="commonName" value="common"/>
</bean>
<bean class="org.xrq.bean.processor.InstantiationAwareBeanPostProcessorBean" />
</beans>
Enter InstantiationAwareBeanPostProcessorBean.postProcessBeforeInstantiation() Enter CommonBean's constructor Enter InstantiationAwareBeanPostProcessorBean.postProcessAfterInstantiation() Enter CommonBean.setCommonName(), commonName = common Enter InstantiationAwareBeanPostProcessorBean.postProcessBeforeInitialization() Enter InstantiationAwareBeanPostProcessorBean.postProcessAfterInitialization() Enter InstantiationAwareBeanPostProcessorBean.postProcessAfterInstantiation() Enter InstantiationAwareBeanPostProcessorBean.postProcessBeforeInitialization() Enter InstantiationAwareBeanPostProcessorBean.postProcessAfterInitialization()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有