属性填充底层源码深入剖析前戏
方式一:使用set方式注入
创建两个类:
package com.coding.spring.practies;
public class TestDIBean {
public String say() {
return "I am TestDIBean.say()";
}
}
package com.coding.spring.practies;
public class TestDIBean2 {
private TestDIBean testDIBean;
public TestDIBean getTestDIBean() {
return testDIBean;
}
public void setTestDIBean(TestDIBean testDIBean) {
this.testDIBean = testDIBean;
}
public void print() {
System.out.println(this.getClass() + "---->>" + testDIBean.say());
}
}
创建spring.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="testDIBean" class="com.coding.spring.practies.TestDIBean"/>
<bean id="testDIBean1" class="com.coding.spring.practies.TestDIBean1">
<!--一定要进行添加,否则注入不进去-->
<property name="testDIBean" ref="testDIBean"></property>
</bean>
</beans>
编写测试类:
@Test
public void obtainBeanByPureAnnotation2(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-practies.xml");
TestDIBean2 testDIBean2 = (TestDIBean2)context.getBean(TestDIBean2.class);
testDIBean2.print();
}
output: class com.coding.spring.practies.TestDIBean1---->>I am TestDIBean.say()
方式二:使用注解扫描的方式
创建两个类:
package com.coding.spring.practies;
import org.springframework.stereotype.Component;
@Component
public class TestDIAutoScanBean {
public String say() {
return "I am TestDIAutoScanBean.say()";
}
}
package com.coding.spring.practies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestDIAutoScanBean1 {
// 将@Autowired注解添加到类的属性字段上
// 完成自动注入
@Autowired
private TestDIAutoScanBean testDIAutoScanBean;
public void print(){
System.out.println(this.getClass() + "---> " + testDIAutoScanBean.say());
}
}
编写测试类:
@Test
public void obtainBeanByPureAnnotation3(){
ApplicationContext context = new AnnotationConfigApplicationContext("com.coding.spring.practies");
TestDIAutoScanBean1 testDIAutoScanBean1 = (TestDIAutoScanBean1)context.getBean(TestDIAutoScanBean1.class);
testDIAutoScanBean1.print();
}
output: class com.coding.spring.practies.TestDIAutoScanBean1---> I am TestDIAutoScanBean.say()
方式三:将@Autowired注解添加到set方法上
在方法二的基础上进行改进:
创建两个类:
package com.coding.spring.practies;
import org.springframework.stereotype.Component;
@Component
public class TestDIAutoScanBean {
public String say() {
return "I am TestDIAutoScanBean.say()";
}
}
package com.coding.spring.practies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestDIAutoScanBean2 {
private TestDIAutoScanBean testDIAutoScanBean;
public TestDIAutoScanBean getTestDIAutoScanBean() {
return testDIAutoScanBean;
}
// 注意:将@Autowired添加到set方法上面完成自动注入
@Autowired
public void setTestDIAutoScanBean(TestDIAutoScanBean testDIAutoScanBean) {
this.testDIAutoScanBean = testDIAutoScanBean;
}
public void print(){
System.out.println(this.getClass() + "---> " + testDIAutoScanBean.say());
}
}
编写测试方法:
@Test
public void obtainBeanByPureAnnotation4(){
ApplicationContext context = new AnnotationConfigApplicationContext("com.coding.spring.practies");
TestDIAutoScanBean2 testDIAutoScanBean2 = (TestDIAutoScanBean2)context.getBean(TestDIAutoScanBean2.class);
testDIAutoScanBean2.print();
}
output: class com.coding.spring.practies.TestDIAutoScanBean2---> I am TestDIAutoScanBean.say()
标签:填充,spring,coding,class,剖析,say,源码,practies,public
From: https://www.cnblogs.com/dongyaotou/p/18327349