© 版权声明:本文为博主原创文章,转载请注明出处
Bean的自动装配(Autowiring)
no:不启用自动装配,此时需要手动注入。参考:Spring学习三----------注入方式
default:优先执行constructor,然后byType,最后byName
byName:根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配
byType:如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配;如果存在多个该类型bean,那么跑出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的bean,则说明事都不发生
constructor:与byType方式类似,不同之处在于它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springbean</groupId>
<artifactId>Spring-AutoAssemble</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-AutoAssemble Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<spring.version>4.3.7.RELEASE</spring.version>
</properties> <dependencies>
<!-- junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies> <build>
<finalName>Spring-AutoAssemble</finalName>
</build>
</project>
3.AutowiringDao.java
package org.spring.autowiring.dao; public interface AutowiringDao { public void say(String word); }
4.AutowiringDaoImpl.java
package org.spring.autowiring.dao.impl; import org.spring.autowiring.dao.AutowiringDao; public class AutowiringDaoImpl implements AutowiringDao { public void say(String word) { System.out.println("AutowiringDaoImpl接受参数:" + word); } }
5.AutowiringService.java
package org.spring.autowiring.service; public interface AutowiringService { public void say(String word); }
6.AutowiringServiceImpl.java
6.1 byName和byType
package org.spring.autowiring.service.impl; import org.spring.autowiring.dao.AutowiringDao;
import org.spring.autowiring.service.AutowiringService; public class AutowiringServiceImpl implements AutowiringService { //byName时,必须与bean配置的id一致;byType时,可随意命名。本次修改bean的id,不再修改该属性名;效果一样
private AutowiringDao autowiringDao; /**
* (byName和byType)只需setter方法
* @param autowiringDao
*/
public void setAutowiringDao(AutowiringDao autowiringDao) { this.autowiringDao = autowiringDao; } public void say(String word) { System.out.println("AutowiringServiceImpl接受参数:" + word);
autowiringDao.say(word); } }
6.2 constructor
package org.spring.autowiring.service.impl; import org.spring.autowiring.dao.AutowiringDao;
import org.spring.autowiring.service.AutowiringService; public class AutowiringServiceImpl implements AutowiringService { private AutowiringDao autowiringDao; /**
* constructor时存在;byName和byType时存在会报错
*
* @param autowiringDao
*/
public AutowiringServiceImpl(AutowiringDao autowiringDao) { this.autowiringDao = autowiringDao; } public void say(String word) { System.out.println("AutowiringServiceImpl接受参数:" + word);
autowiringDao.say(word); } }
7.spring-autowiring.xml(三选一)
7.1 byName
<?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="org.spring.autowiring.service.impl.AutowiringServiceImpl"/> <bean id="autowiringDao" class="org.spring.autowiring.dao.impl.AutowiringDaoImpl"/> </beans>
7.2 byType和constructor
<?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="org.spring.autowiring.service.impl.AutowiringServiceImpl"/> <bean class="org.spring.autowiring.dao.impl.AutowiringDaoImpl"/> </beans>
8.JunitTestBase.java
package org.spring.autowiring.test; import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; /**
* 单元测试基类
*
*/
public class JunitTestBase { private ClassPathXmlApplicationContext context;
private String springXmlPath; /**
* 无参构造器,使用此构造器创建对象,使用默认spring配置文件
*/
public JunitTestBase() { } /**
* 含参构造器,初始化spring配置文件路径
*
* @param springXmlPath
* spring配置文件路径
*/
public JunitTestBase(String springXmlPath) { this.springXmlPath = springXmlPath; } /**
* 启动spring容器,在@Test方法执行之前执行
*/
@Before
public void start() { if(StringUtils.isEmpty(springXmlPath)) {//设置默认spring配置文件路径
springXmlPath = "classpath:spring-*.xml";
}
//加载spring配置文件到spring容器中
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
//启动spring容器,并扩散启动信号到该容器下的所有组件
context.start(); } /**
* 销毁spring容器,在@Test方法执行之前执行
*/
@After
public void stop() { if(context != null){
context.destroy();
} } /**
* 通过bean id获取bean对象
*
* @param beanId
* bean id
* @return
*/
public Object getBean(String beanId) { return context.getBean(beanId); } }
9.TestAutowiring.java
package org.spring.autowiring.test; import org.junit.Test;
import org.spring.autowiring.service.AutowiringService; public class TestAutowiring extends JunitTestBase { /**
* 通过构造器传递spring配置文件路径
*/
public TestAutowiring() { super("classpath:spring-autowiring.xml"); } @Test
public void testAutowiring() { AutowiringService autowiringService =
(AutowiringService) super.getBean("autowiringService");
autowiringService.say("test"); } }
10.效果预览
10.1 byName或byType(AutowiringServiceImpl.java中含有自定义构造器时)
10.2 byName(AutowiringServiceImpl.java中不含有自定义构造器时)
10.3 byType(spring-autowiring.xml中dao层配置的bean中id不存在时)
10.4 byType(spring-autowiring.xml中dao层配置的bean中id存在时)---只是样式不一样而已。。。
10.5 constructor
更多测试情况,可自行测试。