Skip to content

Commit 6f938d2

Browse files
committed
Readme
#1.对spring源码进行测试,在spring-context模块中添加测试代码 spring-projects#2.对spring的bean生命周期进行调试,具体代码在spring-context模块中
1 parent f3e860e commit 6f938d2

File tree

10 files changed

+399
-0
lines changed

10 files changed

+399
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.debug.beanlifecycle;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
5+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6+
7+
/**
8+
* @author: Shawn Chen
9+
* @date: 2018/6/6
10+
* @description:自定义BeanFactoryPostProcessor实现类
11+
*/
12+
public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor
13+
{
14+
@Override
15+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
16+
{
17+
18+
System.out.println("1.调用BeanFactoryPostProcessor的postProcessBeanFactory()方法");
19+
System.out.println();
20+
System.out.println("1.postProcessBeanFactory()方法在工厂处理器后,ApplicationContext容器初始化中refresh()中调用");
21+
System.out.println();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.debug.beanlifecycle;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.*;
5+
import org.springframework.context.*;
6+
import org.springframework.core.env.Environment;
7+
import org.springframework.core.io.Resource;
8+
import org.springframework.core.io.ResourceLoader;
9+
10+
import java.util.Arrays;
11+
12+
/**
13+
* @author: Shawn Chen
14+
* @date: 2018/6/6
15+
* @description:测试主类
16+
*/
17+
public class BeanLifeCycle implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, EnvironmentAware, ResourceLoaderAware
18+
{
19+
20+
private String name;
21+
22+
public String getName()
23+
{
24+
return name;
25+
}
26+
27+
public void setName(String name)
28+
{
29+
System.out.println("6.setName(name),属性注入后调用,此时name=" + name);
30+
System.out.println();
31+
this.name = name;
32+
}
33+
34+
public BeanLifeCycle()
35+
{
36+
System.out.println("3.调用BeanLifeCycle无参构造函数,进行实例化");
37+
System.out.println();
38+
}
39+
40+
@Override
41+
public void afterPropertiesSet() throws Exception
42+
{
43+
//processBeforeInitialization(BeanPostProcessor)后调用
44+
System.out.println("11.调用InitializingBean的afterPropertiesSet(),processBeforeInitialization之后,配置的initMethod之前调用");
45+
System.out.println();
46+
}
47+
48+
@Override
49+
public void destroy() throws Exception
50+
{
51+
System.out.println("16.执行DisposableBean接口的destroy方法");
52+
System.out.println();
53+
}
54+
55+
56+
/**
57+
* 通过<bean>的destroy-method属性指定的销毁方法
58+
*
59+
* @throws Exception
60+
*/
61+
public void destroyMethod() throws Exception
62+
{
63+
System.out.println("17.执行配置的destroy-method()方法");
64+
System.out.println();
65+
}
66+
67+
/**
68+
* 通过<bean>的init-method属性指定的初始化方法
69+
*
70+
* @throws Exception
71+
*/
72+
public void initMethod() throws Exception
73+
{
74+
System.out.println("12.BeanLifyCycle执行配置的init-method()");
75+
System.out.println();
76+
}
77+
78+
@Override
79+
public void setBeanClassLoader(ClassLoader classLoader)
80+
{
81+
System.out.println("执行setBeanClassLoader,ClassLoader Name = " + classLoader.getClass().getName());
82+
System.out.println();
83+
}
84+
85+
@Override
86+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException
87+
{
88+
//setBeanName 后调用
89+
System.out.println("8.调用BeanFactoryAware的setBeanFactory()方法,setBeanName后调用,BeanLifeCycle bean singleton=" + beanFactory.isSingleton("beanlifecycle"));
90+
System.out.println();
91+
}
92+
93+
@Override
94+
public void setBeanName(String name)
95+
{
96+
System.out.println("7.调用BeanNameAware的setBeanName()方法,设置Bean名字:: Bean Name defined in context=" + name);
97+
System.out.println();
98+
}
99+
100+
101+
@Override
102+
public void setEnvironment(Environment environment)
103+
{
104+
System.out.println("执行setEnvironment");
105+
System.out.println();
106+
}
107+
108+
@Override
109+
public void setResourceLoader(ResourceLoader resourceLoader)
110+
{
111+
112+
Resource resource = resourceLoader.getResource("classpath:beanlifecycle.xml");
113+
System.out.println("执行setResourceLoader:: Resource File Name=" + resource.getFilename());
114+
System.out.println();
115+
}
116+
117+
@Override
118+
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
119+
{
120+
System.out.println("执行setApplicationEventPublisher");
121+
System.out.println();
122+
}
123+
124+
@Override
125+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
126+
{
127+
System.out.println("执行setApplicationContext:: Bean Definition Names=" + Arrays.toString(applicationContext.getBeanDefinitionNames()));
128+
System.out.println();
129+
}
130+
131+
132+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.debug.beanlifecycle;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.beans.factory.config.BeanPostProcessor;
5+
6+
/**
7+
* @author: Shawn Chen
8+
* @date: 2018/6/6
9+
* @description:自定义BeanPostProcessor实现类
10+
*/
11+
public class CustomerBeanPostProcessor implements BeanPostProcessor
12+
{
13+
14+
@Override
15+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
16+
{
17+
System.out.println("9.调用BeanPostProcessor中的postProcessBeforeInitialization()方法, beanName= " + beanName);
18+
System.out.println();
19+
return bean;
20+
}
21+
22+
@Override
23+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
24+
{
25+
System.out.println("13.执行BeanPostProcessor的postProcessAfterInitialization()方法,beanName=" + beanName);
26+
System.out.println();
27+
return bean;
28+
}
29+
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.debug.beanlifecycle;
2+
3+
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.beans.PropertyValues;
6+
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
7+
8+
import java.beans.PropertyDescriptor;
9+
import java.util.Arrays;
10+
11+
/**
12+
* @author: Shawn Chen
13+
* @date: 2018/6/6
14+
* @description:自定义InstantiationAwareBeanPostProcessor类
15+
*/
16+
public class InstanceBeanPostProcessor implements InstantiationAwareBeanPostProcessor
17+
{
18+
@Override
19+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
20+
{
21+
System.out.println("10.调用InstantiationAwareBeanPostProcessor中的postProcessBeforeInitialization()方法, beanName = " + beanName);
22+
System.out.println();
23+
return bean;
24+
}
25+
26+
@Override
27+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
28+
{
29+
System.out.println("14.执行InstantiationAwareBeanPostProcessor的postProcessAfterInitialization()方法");
30+
System.out.println();
31+
return bean;
32+
}
33+
34+
@Override
35+
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException
36+
{
37+
System.out.println("4.调用InstantiationAwareBeanPostProcessor中的postProcessAfterInstantiation()方法");
38+
System.out.println();
39+
System.out.println("4.返回boolean,bean实例化后调用,并且返回false则不会注入属性,beanName" + beanName);
40+
System.out.println();
41+
return true;
42+
}
43+
44+
45+
@Override
46+
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException
47+
{
48+
System.out.println("2.调用InstantiationAwareBeanPostProcessor中的postProcessBeforeInstantiation()方法");
49+
System.out.println();
50+
System.out.println("2.实例化bean之前调用,即调用bean类构造函数之前调用 " + beanClass.getName());
51+
System.out.println();
52+
return null;
53+
}
54+
55+
@Override
56+
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException
57+
{
58+
System.out.println("5.调用InstantiationAwareBeanPostProcessor中的postProcessPropertyValues()方法");
59+
System.out.println();
60+
System.out.println("5.postProcessPropertyValues,在属性注入之前调用...... beanName = " + beanName + " 属性名集合 : " + Arrays.toString(pvs.getPropertyValues()));
61+
System.out.println();
62+
System.out.println("这里Bean的name属性还是null:" + ((BeanLifeCycle) bean).getName()); //这里可以看到nam的值
63+
System.out.println();
64+
return pvs;//这里要返回propertyValues,否则属性无法注入
65+
}
66+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.debug.spring;
2+
3+
/**
4+
* @author: Shawn Chen
5+
* @date: 2018/6/6
6+
* @description:
7+
*/
8+
//@Component
9+
public class User
10+
{
11+
//@Value(value = "github")
12+
private String name;
13+
// @Value(value = "male")
14+
private String gender;
15+
16+
public String getName()
17+
{
18+
return name;
19+
}
20+
21+
public void setName(String name)
22+
{
23+
this.name = name;
24+
}
25+
26+
public String getGender()
27+
{
28+
return gender;
29+
}
30+
31+
public void setGender(String gender)
32+
{
33+
this.gender = gender;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:p="http://www.springframework.org/schema/p"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
10+
11+
<bean id="user" class="com.debug.spring.User"
12+
p:name="测试" p:gender="保密"/>
13+
14+
<!-- <context:component-scan base-package="com.debug.spring"/>-->
15+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
7+
<bean id="beanlifecycle" class="com.debug.beanlifecycle.BeanLifeCycle" init-method="initMethod"
8+
destroy-method="destroyMethod">
9+
<property name="name" value="小测"></property>
10+
</bean>
11+
12+
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
13+
<bean class="com.debug.beanlifecycle.CustomerBeanPostProcessor"/>
14+
<bean class="com.debug.beanlifecycle.BeanFactoryPostProcessorTest"/>
15+
<bean class="com.debug.beanlifecycle.InstanceBeanPostProcessor"/>
16+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
7+
<context:component-scan base-package="com.debug.beanlifecycle"/>
8+
9+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.debug.beanlifecycletest;
2+
3+
import com.debug.beanlifecycle.BeanLifeCycle;
4+
import org.junit.Test;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.support.ClassPathXmlApplicationContext;
7+
8+
/**
9+
* @author: Shawn Chen
10+
* @date: 2018/6/6
11+
* @description:
12+
*/
13+
public class LifeCycleTest
14+
{
15+
16+
@Test
17+
public void testBeanLifeCycle()
18+
{
19+
20+
System.out.println("Bean生命周期:");
21+
System.out.println("Spring容器初始化");
22+
System.out.println("=====================================");
23+
24+
ApplicationContext context = new ClassPathXmlApplicationContext("com/debug/config/beanlifecycle.xml");
25+
26+
System.out.println("Spring容器初始化完毕");
27+
System.out.println("=====================================");
28+
29+
System.out.println("从容器中获取Bean");
30+
System.out.println();
31+
32+
BeanLifeCycle beanLifeCycle = (BeanLifeCycle) context.getBean("beanlifecycle");
33+
34+
System.out.println("15.初始化成功,并且属性注入成功:beanLifyCycle Name=" + beanLifeCycle.getName());
35+
System.out.println("=====================================");
36+
37+
((ClassPathXmlApplicationContext) context).close();
38+
System.out.println("=====================================");
39+
System.out.println("Spring容器关闭,Bean生命周期结束!");
40+
41+
}
42+
43+
}

0 commit comments

Comments
 (0)