整體流程圖
目前創(chuàng)新互聯(lián)公司已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、龍湖網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
程序示例
maven依賴(lài)
org.springframework
spring-context
4.3.12.RELEASE
junit
junit
4.12
test
配置類(lèi)
@Configuration
@ComponentScan("per.ym.processor, per.ym.service")
public class LifeCycleConfig {
@Bean(initMethod = "init", destroyMethod = "destroy")
public LifeCycleBean lifeCycleBean() {
return new LifeCycleBean();
}
}
實(shí)例bean
public class LifeCycleBean implements InitializingBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {
public LifeCycleBean() {
System.out.println("consturctor......");
}
public void init() {
System.out.println("init......");
}
@Autowired
public void setService(PersonService service) {
System.out.println("setService......");
}
public void destroy() {
System.out.println("destroy......");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet......");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("setBeanFactory......");
}
@Override
public void setBeanName(String name) {
System.out.println("setBeanName......");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("setBeanClassLoader......");
}
}
InstantiationAwareBeanPostProcessor
package per.ym.processor;
@Component
public class MyInstantiationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
@Override
public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessBeforeInstantiation......");
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessAfterInstantiation......");
}
return true;
}
@Override
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessPropertyValues......");
}
return pvs;
}
}
BeanPostProcessor
package per.ym.processor;
@Component
public class MyPostProcess implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessBeforeInitialization......");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("lifeCycleBean")) {
System.out.println("postProcessAfterInitialization......");
}
return bean;
}
}
service
package per.ym.service;
@Service
public class Service {
}
測(cè)試類(lèi)
public class Test_Normal {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
@Test
public void test() {
applicationContext.getBean("lifeCycleBean");
applicationContext.close();
}
}
測(cè)試結(jié)果:
postProcessBeforeInstantiation......
consturctor......
postProcessAfterInstantiation......
postProcessPropertyValues......
setService......
setBeanName......
setBeanClassLoader......
setBeanFactory......
postProcessBeforeInitialization......
afterPropertiesSet......
init......
postProcessAfterInitialization......
destroy......