Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
来源:互联网 发布:淘宝平面模特怎么入行 编辑:程序博客网 时间:2024/05/18 02:28
基于构造器的注入方式
package com.spring.test.dao;
public class PersonDao {
public void add() {
System.out.println("This is add() method in DAO layer.");
}
}
--------------------------------------------------------------------------
package com.spring.test.manager.impl;
import com.spring.test.dao.PersonDao;
import com.spring.test.manager.IPersonService;
public class PersonManager implements IPersonService {
private PersonDao personDao;
private String name;
public PersonManager() {
}
public PersonManager(PersonDao personDao, String name) {
super();
this.personDao = personDao;
this.name = name;
}
@Override
public void save() {
personDao.add();
System.out.println("name is " + name);
}
}
该类的构造函数有2个属性,一个是类PersonDao,一个是String类型的变量。spring关于构造器注入的配置如下:
<?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-3.0.xsd">
<bean id="personDao" class="com.spring.test.dao.PersonDao" />
<bean id="personService" class="com.spring.test.manager.impl.PersonManager">
<constructor-arg index="0" type="com.spring.test.dao.PersonDao"
ref="personDao" />
<constructor-arg index="1" type="java.lang.String"
value="name string" />
</bean>
</beans>
这是关于constructor-arg的设置,其作用就是通过构造器注入依赖的对象或基本数据类型的值。
index从0开始计数,其顺序要与类中的参数一一对应。
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.manager.IPersonService;
public class PersonServiceTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonService personService = (IPersonService) ctx
.getBean("personService");
personService.save();
}
}
运行该测试类,可看到正确的注入结果。
基于构造器的注入还是基于Setter方式的注入?
推荐采用Setter的注入方式,以下内容来自spring的参考文档
Constructor-based or setter-based DI?
Since you can mix both, Constructor- and Setter-based DI, it is a good rule of thumb to use constructor arguments for mandatory dependencies and setters for optional dependencies. Note that the use of a @Required annotation on a setter can be used to make setters required dependencies.
The Spring team generally advocates setter injection, because large numbers of constructor arguments can get unwieldy, especially when properties are optional. Setter methods also make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is a compelling use case.
Some purists favor constructor-based injection. Supplying all object dependencies means that the object is always returned to client (calling) code in a totally initialized state. The disadvantage is that the object becomes less amenable to reconfiguration and re-injection.
Use the DI that makes the most sense for a particular class. Sometimes, when dealing with third-party classes to which you do not have the source, the choice is made for you. A legacy class may not expose any setter methods, and so constructor injection is the only available DI.
- Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
- Spring v3.0.2 Learning Note 8 - Annotation-based dependency injection
- Spring v3.0.2 Learning Note 10 - Annotation-based Transaction Management
- Spring v3.0.2 Learning Note 11 - XML-based Transaction Management
- Setter-based dependency injection
- Setter-based dependency injection
- Spring v3.0.2 Learning Note 1 - Introduction to Spring Framework
- Spring v3.0.2 Learning Note 14 - Send Email with Spring
- Spring v3.0.2 Learning Note 2 - A Sample Example
- Spring v3.0.2 Learning Note 3 - Initialisation of Beans
- Spring v3.0.2 Learning Note 4 - Scope of Beans
- Spring v3.0.2 Learning Note 5 - Lifecycle of Beans
- Spring v3.0.2 Learning Note 6 - Inject Components
- Spring v3.0.2 Learning Note 9 - Classpath Scanning
- Spring v3.0.2 Learning Note 12 - Integrate with Hiberate
- Spring v3.0.2 Learning Note 13 - AOP Example
- Spring v3.0.2 Learning Note 16 - Integrate With Hessian
- Spring 3 Dependency injection
- Oracle 连接
- Spring v3.0.2 Learning Note 6 - Inject Components
- Reporting Service参数化报表
- 为谁写代码
- 深入理解sizeof
- Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
- Communications link failure Last packet sent to the server was 0 ms ago
- Spring v3.0.2 Learning Note 8 - Annotation-based dependency injection
- ASP.NET前台代码绑定后台变量方法总结
- Spring v3.0.2 Learning Note 9 - Classpath Scanning
- CTreeCtrl和CListCtrl复杂控件的综合使用
- 在使用Mvc ”FormCollection “ +“ValidationSummary”和“ValidationMessage” 验证数据时的小问题
- 详解Spring事务属性
- NOKIA最全常识+技术+经验