欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring:Bean的自动装配AutoWiring

程序员文章站 2024-03-13 17:23:03
...

首先一张图,说明都有什么装配方式:

Spring:Bean的自动装配AutoWiring

下面分别给出byName,byType和Constructor三种自动装配方式的例子。

1.byName

//1.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" 
        default-autowire="byName">
        
        <bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>
        <bean id="autoWiringDAO" class="com.imooc.autowiring.AutoWiringDAO" ></bean>
 </beans>

//2.Service类
package com.imooc.autowiring;

public class AutoWiringService {
	
	private AutoWiringDAO autoWiringDAO;
	//set方法必须加上,写上标识输出语句
	public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) {
		System.out.println("setAutoWiringDAO");
		this.autoWiringDAO = autoWiringDAO;
	}
	
	public void say(String word) {
		this.autoWiringDAO.say(word);
	}

}

//3.DAO类
package com.imooc.autowiring;

public class AutoWiringDAO {
	//只有一个say()方法
	public void say(String word) {
		System.out.println("AutoWiringDAO : " + word);
	}

}

//测试类
package com.imooc.test.autowiring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.autowiring.AutoWiringService;
import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAutoWiring extends UnitTestBase {
	
	public TestAutoWiring() {
		super("classpath:spring-autowiring.xml");
	}
	
	@Test
	public void testSay() {
		AutoWiringService service = super.getBean("autoWiringService");
		service.say(" this is a test");
	}

}

输出结果为:

8月 02, 2018 2:08:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing orgaaa@qq.com146044d7: startup date [Thu Aug 02 14:08:53 CST 2018]; root of context hierarchy
8月 02, 2018 2:08:53 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-autowiring.xml]
setAutoWiringDAO
AutoWiringDAO :  this is a test
8月 02, 2018 2:08:54 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing orgaaa@qq.com146044d7: startup date [Thu Aug 02 14:08:53 CST 2018]; root of context hierarchy

从输出结果可以看出,通过name的方式在bean容器中自动查找到了autoWiringDAO对象,并且注入到了service的set方法中,第一句输出可以看出是通过set方法注入的。

2.byType方式

这个其他部分和byName方式一样,只是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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" 
        default-autowire="byType">
        
        <bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean> 
        <bean  class="com.imooc.autowiring.AutoWiringDAO" ></bean>
 </beans>

xml文件中还去掉了类AutoWiringDAO的id,实验结果

8月 02, 2018 2:22:22 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing orgaaa@qq.com146044d7: startup date [Thu Aug 02 14:22:22 CST 2018]; root of context hierarchy
8月 02, 2018 2:22:22 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-autowiring.xml]
setAutoWiringDAO
AutoWiringDAO :  this is a test
8月 02, 2018 2:22:22 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.contaaa@qq.com: startup date [Thu Aug 02 14:22:22 CST 2018]; root of context hierarchy

一样,在bean容器中找到了对应的bean并注入到set方法。

3.constructor方式,这种方式采用的是type形式的寻找,所以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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" 
        default-autowire="byType">
        
        <bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>
        <bean  class="com.imooc.autowiring.AutoWiringDAO" ></bean>
 </beans>

同时,service类里面加入构造函数

package com.imooc.autowiring;

public class AutoWiringService {
	
	private AutoWiringDAO autoWiringDAO;
	
	public AutoWiringService(AutoWiringDAO autoWiringDAO) {
		System.out.println("AutoWiringService");
		this.autoWiringDAO = autoWiringDAO;
	}

	public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) {
		System.out.println("setAutoWiringDAO");
		this.autoWiringDAO = autoWiringDAO;
	}
	
	public void say(String word) {
		this.autoWiringDAO.say(word);
	}

}

输出结果为:

8月 02, 2018 2:24:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing orgaaa@qq.com146044d7: startup date [Thu Aug 02 14:24:53 CST 2018]; root of context hierarchy
8月 02, 2018 2:24:53 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-autowiring.xml]
AutoWiringService
AutoWiringDAO :  this is a test
8月 02, 2018 2:24:53 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing orgaaa@qq.com146044d7: startup date [Thu Aug 02 14:24:53 CST 2018]; root of context hierarchy

从结果信息看出,这种方式在没有id的情况下,仍然找到了相应的bean,验证了type寻找方式。

同时可以看出,这次是注入到service的构造函数,而不是set函数。