屬性填充底層原始碼深入剖析前戲

~码铃薯~發表於2024-07-27

屬性填充底層原始碼深入剖析前戲

方式一:使用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()

相關文章