有参构造函数注入底层源码深入剖析**前戏

有参构造函数注入底层源码深入剖析前戏

方式一:

创建两个类:

public class TestDIBean {
	public String say() {
		return "I am TestDIBean.say()";
	}
}
package com.coding.spring.practies;

public class TestDIBean1 {
	private TestDIBean testDIBean;

	public TestDIBean1(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">
		<!--一定要进行添加,否则注入不进去-->
		<constructor-arg ref="testDIBean"></constructor-arg>
	</bean>

</beans>

编写测试类:

@Test
	public void obtainBeanByPureAnnotation(){
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-practies.xml");
		TestDIBean1 testDIBean1 = (TestDIBean1)context.getBean("testDIBean1");
		testDIBean1.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 {

	private TestDIAutoScanBean testDIAutoScanBean;

	@Autowired
	public TestDIAutoScanBean1(TestDIAutoScanBean testDIAutoScanBean) {
		this.testDIAutoScanBean = testDIAutoScanBean;
	}

	public void print(){
		System.out.println(this.getClass() + "---->>" + testDIAutoScanBean.say());
	}
}

编写测试类:

@Test
	public void obtainBeanByPureAnnotation2(){
		ApplicationContext context = new AnnotationConfigApplicationContext("com.coding.spring.practies");
		TestDIAutoScanBean1 testDIAutoScanBean1 = (TestDIAutoScanBean1)context.getBean("testDIAutoScanBean1");
		testDIAutoScanBean1.print();
	}
output: class com.coding.spring.practies.TestDIAutoScanBean1---->>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 {

	private TestDIAutoScanBean testDIAutoScanBean;

	// 将@Autowired注解注释掉也是能狗进行正常注入的,运行结果不受影响
//	@Autowired
	public TestDIAutoScanBean1(TestDIAutoScanBean testDIAutoScanBean) {
		this.testDIAutoScanBean = testDIAutoScanBean;
	}

	public void print(){
		System.out.println(this.getClass() + "---->>" + testDIAutoScanBean.say());
	}
}

posted on 2024-07-27 17:00  ~码铃薯~  阅读(5)  评论(0编辑  收藏  举报

导航