diff --git a/spring-context/src/main/java/com/debug/beanlifecycle/BeanFactoryPostProcessorTest.java b/spring-context/src/main/java/com/debug/beanlifecycle/BeanFactoryPostProcessorTest.java new file mode 100644 index 000000000000..31efd2a7672b --- /dev/null +++ b/spring-context/src/main/java/com/debug/beanlifecycle/BeanFactoryPostProcessorTest.java @@ -0,0 +1,23 @@ +package com.debug.beanlifecycle; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description:自定义BeanFactoryPostProcessor实现类 + */ +public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor +{ + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException + { + + System.out.println("1.调用BeanFactoryPostProcessor的postProcessBeanFactory()方法"); + System.out.println(); + System.out.println("1.postProcessBeanFactory()方法在工厂处理器后,ApplicationContext容器初始化中refresh()中调用"); + System.out.println(); + } +} diff --git a/spring-context/src/main/java/com/debug/beanlifecycle/BeanLifeCycle.java b/spring-context/src/main/java/com/debug/beanlifecycle/BeanLifeCycle.java new file mode 100644 index 000000000000..921211ad2dcd --- /dev/null +++ b/spring-context/src/main/java/com/debug/beanlifecycle/BeanLifeCycle.java @@ -0,0 +1,136 @@ +package com.debug.beanlifecycle; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.*; +import org.springframework.context.*; +import org.springframework.core.env.Environment; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; + +import java.util.Arrays; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description:测试主类 + */ +public class BeanLifeCycle implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, EnvironmentAware, ResourceLoaderAware +{ + + private String name; + + public String getName() + { + return name; + } + + public void setName(String name) + { + System.out.println("6.setName(name),属性注入后调用,此时name=" + name); + System.out.println(); + this.name = name; + } + + public BeanLifeCycle() + { + System.out.println("3.调用BeanLifeCycle无参构造函数,进行实例化"); + System.out.println(); + } + + @Override + public void afterPropertiesSet() throws Exception + { + //processBeforeInitialization(BeanPostProcessor)后调用 + System.out.println("11.调用InitializingBean的afterPropertiesSet(),processBeforeInitialization之后,配置的initMethod之前调用"); + System.out.println(); + } + + @Override + public void destroy() throws Exception + { + System.out.println("16.执行DisposableBean接口的destroy方法"); + System.out.println(); + } + + + /** + * 通过的destroy-method属性指定的销毁方法 + * + * @throws Exception + */ + public void destroyMethod() throws Exception + { + System.out.println("17.执行配置的destroy-method()方法"); + System.out.println(); + } + + /** + * 通过的init-method属性指定的初始化方法 + * + * @throws Exception + */ + public void initMethod() throws Exception + { + System.out.println("12.BeanLifyCycle执行配置的init-method()"); + System.out.println(); + } + + @Override + public void setBeanClassLoader(ClassLoader classLoader) + { + System.out.println("执行setBeanClassLoader,ClassLoader Name = " + classLoader.getClass().getName()); + System.out.println(); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException + { + //setBeanName 后调用 + System.out.println("8.调用BeanFactoryAware的setBeanFactory()方法,setBeanName后调用,BeanLifeCycle bean singleton=" + beanFactory.isSingleton("beanlifecycle")); + System.out.println(); + } + + @Override + public void setBeanName(String name) + { + System.out.println("7.调用BeanNameAware的setBeanName()方法,设置Bean名字:: Bean Name defined in context=" + name); + System.out.println(); + } + + + /** + * 这里继承了EnvironmentAware类,具体可参看AbstractApplicationContext的prepareBeanFactory方法里面有具体注册 + * @param environment + */ + @Override + public void setEnvironment(Environment environment) + { + System.out.println("执行setEnvironment"); + System.out.println(); + } + + @Override + public void setResourceLoader(ResourceLoader resourceLoader) + { + + Resource resource = resourceLoader.getResource("classpath:beanlifecycle.xml"); + System.out.println("执行setResourceLoader:: Resource File Name=" + resource.getFilename()); + System.out.println(); + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) + { + System.out.println("执行setApplicationEventPublisher"); + System.out.println(); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + System.out.println("执行setApplicationContext:: Bean Definition Names=" + Arrays.toString(applicationContext.getBeanDefinitionNames())); + System.out.println(); + } + + +} diff --git a/spring-context/src/main/java/com/debug/beanlifecycle/CustomerBeanPostProcessor.java b/spring-context/src/main/java/com/debug/beanlifecycle/CustomerBeanPostProcessor.java new file mode 100644 index 000000000000..6cd044bf8a36 --- /dev/null +++ b/spring-context/src/main/java/com/debug/beanlifecycle/CustomerBeanPostProcessor.java @@ -0,0 +1,31 @@ +package com.debug.beanlifecycle; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description:自定义BeanPostProcessor实现类 + */ +public class CustomerBeanPostProcessor implements BeanPostProcessor +{ + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException + { + System.out.println("9.调用BeanPostProcessor中的postProcessBeforeInitialization()方法, beanName= " + beanName); + System.out.println(); + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException + { + System.out.println("13.执行BeanPostProcessor的postProcessAfterInitialization()方法,beanName=" + beanName); + System.out.println(); + return bean; + } + + +} diff --git a/spring-context/src/main/java/com/debug/beanlifecycle/InstanceBeanPostProcessor.java b/spring-context/src/main/java/com/debug/beanlifecycle/InstanceBeanPostProcessor.java new file mode 100644 index 000000000000..8e7fa116ab59 --- /dev/null +++ b/spring-context/src/main/java/com/debug/beanlifecycle/InstanceBeanPostProcessor.java @@ -0,0 +1,66 @@ +package com.debug.beanlifecycle; + + +import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyValues; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; + +import java.beans.PropertyDescriptor; +import java.util.Arrays; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description:自定义InstantiationAwareBeanPostProcessor类 + */ +public class InstanceBeanPostProcessor implements InstantiationAwareBeanPostProcessor +{ + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException + { + System.out.println("10.调用InstantiationAwareBeanPostProcessor中的postProcessBeforeInitialization()方法, beanName = " + beanName); + System.out.println(); + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException + { + System.out.println("14.执行InstantiationAwareBeanPostProcessor的postProcessAfterInitialization()方法"); + System.out.println(); + return bean; + } + + @Override + public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException + { + System.out.println("4.调用InstantiationAwareBeanPostProcessor中的postProcessAfterInstantiation()方法"); + System.out.println(); + System.out.println("4.返回boolean,bean实例化后调用,并且返回false则不会注入属性,beanName" + beanName); + System.out.println(); + return true; + } + + + @Override + public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException + { + System.out.println("2.调用InstantiationAwareBeanPostProcessor中的postProcessBeforeInstantiation()方法"); + System.out.println(); + System.out.println("2.实例化bean之前调用,即调用bean类构造函数之前调用 " + beanClass.getName()); + System.out.println(); + return null; + } + + @Override + public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException + { + System.out.println("5.调用InstantiationAwareBeanPostProcessor中的postProcessPropertyValues()方法"); + System.out.println(); + System.out.println("5.postProcessPropertyValues,在属性注入之前调用...... beanName = " + beanName + " 属性名集合 : " + Arrays.toString(pvs.getPropertyValues())); + System.out.println(); + System.out.println("这里Bean的name属性还是null:" + ((BeanLifeCycle) bean).getName()); //这里可以看到nam的值 + System.out.println(); + return pvs;//这里要返回propertyValues,否则属性无法注入 + } +} diff --git a/spring-context/src/main/java/com/debug/spring/User.java b/spring-context/src/main/java/com/debug/spring/User.java new file mode 100644 index 000000000000..9141a07c2893 --- /dev/null +++ b/spring-context/src/main/java/com/debug/spring/User.java @@ -0,0 +1,35 @@ +package com.debug.spring; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description: + */ +//@Component +public class User +{ + //@Value(value = "github") + private String name; + // @Value(value = "male") + private String gender; + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getGender() + { + return gender; + } + + public void setGender(String gender) + { + this.gender = gender; + } +} diff --git a/spring-context/src/main/resources/com/debug/config/User.xml b/spring-context/src/main/resources/com/debug/config/User.xml new file mode 100644 index 000000000000..c7cc814a1e89 --- /dev/null +++ b/spring-context/src/main/resources/com/debug/config/User.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/spring-context/src/main/resources/com/debug/config/beanlifecycle.xml b/spring-context/src/main/resources/com/debug/config/beanlifecycle.xml new file mode 100644 index 000000000000..3764c6c31686 --- /dev/null +++ b/spring-context/src/main/resources/com/debug/config/beanlifecycle.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-context/src/main/resources/com/debug/config/beanlifecycle_annotation.xml b/spring-context/src/main/resources/com/debug/config/beanlifecycle_annotation.xml new file mode 100644 index 000000000000..1340d4b2028f --- /dev/null +++ b/spring-context/src/main/resources/com/debug/config/beanlifecycle_annotation.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/spring-context/src/test/java/com/debug/beanlifecycletest/LifeCycleTest.java b/spring-context/src/test/java/com/debug/beanlifecycletest/LifeCycleTest.java new file mode 100644 index 000000000000..abc96176d560 --- /dev/null +++ b/spring-context/src/test/java/com/debug/beanlifecycletest/LifeCycleTest.java @@ -0,0 +1,43 @@ +package com.debug.beanlifecycletest; + +import com.debug.beanlifecycle.BeanLifeCycle; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description: + */ +public class LifeCycleTest +{ + + @Test + public void testBeanLifeCycle() + { + + System.out.println("Bean生命周期:"); + System.out.println("Spring容器初始化"); + System.out.println("====================================="); + + ApplicationContext context = new ClassPathXmlApplicationContext("com/debug/config/beanlifecycle.xml"); + + System.out.println("Spring容器初始化完毕"); + System.out.println("====================================="); + + System.out.println("从容器中获取Bean"); + System.out.println(); + + BeanLifeCycle beanLifeCycle = (BeanLifeCycle) context.getBean("beanlifecycle"); + + System.out.println("15.初始化成功,并且属性注入成功:beanLifyCycle Name=" + beanLifeCycle.getName()); + System.out.println("====================================="); + + ((ClassPathXmlApplicationContext) context).close(); + System.out.println("====================================="); + System.out.println("Spring容器关闭,Bean生命周期结束!"); + + } + +} diff --git a/spring-context/src/test/java/com/debug/springtest/SpringDebugTest.java b/spring-context/src/test/java/com/debug/springtest/SpringDebugTest.java new file mode 100644 index 000000000000..af151796e82c --- /dev/null +++ b/spring-context/src/test/java/com/debug/springtest/SpringDebugTest.java @@ -0,0 +1,29 @@ +package com.debug.springtest; + + +import com.debug.spring.User; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author: Shawn Chen + * @date: 2018/6/6 + * @description:spring源码简单测试 + */ +public class SpringDebugTest +{ + @Test + public void test() + { + ApplicationContext context = new ClassPathXmlApplicationContext("com/debug/config/User.xml"); + + User user = (User) context.getBean("user"); + + System.out.println(user.getClass().getName()); + + System.out.println("name属性:" + user.getName()); + System.out.println("gender属性:" + user.getGender()); + + } +}