Skip to content

Commit 40ffdc0

Browse files
author
morewindows0
committed
Debug IOC
#1.对xml配置形式的IOC初始化过程,进行源码调试
1 parent 8adf6e5 commit 40ffdc0

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

spring-context/src/main/java/com/debug/spring/User.java renamed to spring-context/src/main/java/com/debug/basebean/User.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
package com.debug.spring;
1+
package com.debug.basebean;
22

33
/**
44
* @author: Shawn Chen
55
* @date: 2018/6/6
66
* @description:
77
*/
8-
//@Component
98
public class User
109
{
11-
//@Value(value = "github")
1210
private String name;
13-
// @Value(value = "male")
11+
1412
private String gender;
1513

1614
public String getName()

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,11 @@ public Collection<ApplicationListener<?>> getApplicationListeners() {
516516
public void refresh() throws BeansException, IllegalStateException {
517517
synchronized (this.startupShutdownMonitor) {
518518
// Prepare this context for refreshing.
519+
System.out.println("#3.设置上下文活动标志active(会先将上下文标志设置为false),还有进行一些环境配置。");
519520
prepareRefresh();
520521

521522
// Tell the subclass to refresh the internal bean factory.
523+
System.out.println("#4.核心函数AbstractApplicationContext#obtainFreshBeanFactory,功能解析xml,创建BeanFactory");
522524
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
523525

524526
// Prepare the bean factory for use in this context.
@@ -547,6 +549,7 @@ public void refresh() throws BeansException, IllegalStateException {
547549
registerListeners();
548550

549551
// Instantiate all remaining (non-lazy-init) singletons.
552+
//该函数中会对bean进行初始化,利用发射技术。
550553
finishBeanFactoryInitialization(beanFactory);
551554

552555
// Last step: publish corresponding event.

spring-context/src/main/java/org/springframework/context/support/ClassPathXmlApplicationContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ public ClassPathXmlApplicationContext(
139139
throws BeansException {
140140

141141
super(parent);
142+
System.out.println("#1.配置资源文件:"+configLocations[0]);
142143
setConfigLocations(configLocations);
143144
if (refresh) {
145+
System.out.println("#2.进入核心函数:AbstractApplicationContext#refresh");
144146
refresh();
145147
}
146148
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:p="http://www.springframework.org/schema/p"
4-
xmlns:context="http://www.springframework.org/schema/context"
54
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
65
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">
6+
http://www.springframework.org/schema/beans/spring-beans.xsd">
107

11-
<bean id="user" class="com.debug.spring.User"
12-
p:name="测试" p:gender="保密"/>
8+
<bean id="user" class="com.debug.basebean.User"
9+
p:name="IoC" p:gender="保密"/>
1310

14-
<!-- <context:component-scan base-package="com.debug.spring"/>-->
11+
<!--<context:component-scan base-package="com.debug.spring"/>-->
1512
</beans>

spring-context/src/test/java/com/debug/springtest/SpringDebugTest.java renamed to spring-context/src/test/java/com/debug/springioctest/SpringIOCDebug.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
package com.debug.springtest;
1+
package com.debug.springioctest;
22

33

4-
import com.debug.spring.User;
4+
import com.debug.basebean.User;
55
import org.junit.Test;
66
import org.springframework.context.ApplicationContext;
77
import org.springframework.context.support.ClassPathXmlApplicationContext;
88

99
/**
1010
* @author: Shawn Chen
1111
* @date: 2018/6/6
12-
* @description:spring源码简单测试
12+
* @description:spring IOC调试
1313
*/
14-
public class SpringDebugTest
14+
public class SpringIoCDebug
1515
{
1616
@Test
17-
public void test()
17+
public void testIOC()
1818
{
1919
ApplicationContext context = new ClassPathXmlApplicationContext("com/debug/config/User.xml");
2020

2121
User user = (User) context.getBean("user");
2222

23-
System.out.println(user.getClass().getName());
23+
System.out.println("class name:"+user.getClass().getName());
2424

2525
System.out.println("name属性:" + user.getName());
2626
System.out.println("gender属性:" + user.getGender());

0 commit comments

Comments
 (0)