依赖注入(DI)的三种实现方式
本文来探讨一下依赖注入的三种实现方式是哪三种,Spring的支持又怎么样。
首先,依赖注入(DI)的实现方式有三种:设值注入、构造方法注入、接口注入,下面来看看这三种注入方式在Spring中的支持怎么样。
1、设值注入。
①、新建一个项目
②、新建一个User类,实现setter、getter方法
-
package com.bean;
-
-
public class User {
-
String username;
-
String password;
-
public String getUsername() {
-
return username;
-
}
-
public void setUsername(String username) {
-
this.username = username;
-
}
-
public String getPassword() {
-
return password;
-
}
-
public void setPassword(String password) {
-
this.password = password;
-
}
-
-
}
③、新建一个接口,名字为UserDAO,里面包含一个save方法,将信息存入数据库
-
package com.dao;
-
-
import com.bean.User;
-
-
public interface UserDAO {
-
public void save(User user);
-
}
④、新建一个UserDAO的实现类UserDAOImpl,实现save方法,模拟数据库存入
-
package com.dao;
-
-
import com.bean.User;
-
-
public class UserDAOImpl implements UserDAO {
-
-
-
public void save(User user) {
-
// TODO Auto-generated method stub
-
System.out.println(user.getUsername()+"被存储");
-
}
-
-
}
⑤、为了不将直接操作数据库的对象UserDAO暴露给用户,需添加service层,在数据库操作前后添加业务逻辑。
先实现业务逻辑接口UserService:
-
package com.bean;
-
-
public interface UserService {
-
public void add(User user);
-
}
然后再写这个接口的实现类UserServiceImpl:
-
package com.bean;
-
-
import com.dao.UserDAO;
-
-
public class UserServiceImpl implements UserService {
-
-
private UserDAO userDAO;
-
-
public UserDAO getUserDAO() {
-
return userDAO;
-
}
-
-
//设值注入 为UserDAO的注入做准备
-
public void setUserDAO(UserDAO userDAO) {
-
this.userDAO = userDAO;
-
}
-
//实现负责业务逻辑的add方法
-
-
public void add(User user) {
-
// TODO Auto-generated method stub
-
userDAO.save(user);
-
}
-
-
}
⑥、在src下创建Spring配置文件,命名为applicationContext.xml,在其中配置数据访问类和业务逻辑实现类:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
-
xmlns:cache="http://www.springframework.org/schema/cache"
-
xsi:schemaLocation="
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context.xsd
-
http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx.xsd
-
http://www.springframework.org/schema/jdbc
-
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
-
http://www.springframework.org/schema/cache
-
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop.xsd
-
http://www.springframework.org/schema/util
-
http://www.springframework.org/schema/util/spring-util.xsd">
-
<!-- 配置数据访问类,实例名称为u -->
-
<bean id="u" class="com.dao.UserDAOImpl">
-
</bean>
-
<bean id="user" class="com.bean.User">
-
</bean>
-
-
<!-- 设值注入 配置业务逻辑实现类,实例名称为userService-->
-
<bean id="userService" class="com.bean.UserServiceImpl">
-
<!-- 在这里实现注入,注入实例名称为u的实例到该实例的userDAO属性 -->
-
<property name="userDAO">
-
<ref bean="u" />
-
</property>
-
</bean>
-
</beans>
⑦、写一个测试方法进行测试(为了简洁,这里没有加上测试类,也没有引入包,读者写的时候需要注意)
-
//设值注入测试
-
public static void setValDI(){
-
//初始化容器
-
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
-
UserServiceImpl service = (UserServiceImpl)ctx.getBean("userService");
-
User u=(User)ctx.getBean("user");
-
u.setPassword("123456");
-
u.setUsername("test");
-
service.add(u);
-
}
2、构造方法注入
①、修改UserServiceImpl类 加上构造方法
-
package com.bean;
-
-
import com.dao.UserDAO;
-
-
public class UserServiceImpl implements UserService {
-
-
private UserDAO userDAO;
-
-
public UserDAO getUserDAO() {
-
return userDAO;
-
}
-
-
public void setUserDAO(UserDAO userDAO) {
-
this.userDAO = userDAO;
-
}
-
-
//如果需要构造方法注入 则这里必须写上构造方法
-
public UserServiceImpl(UserDAO userDAO) {
-
super();
-
this.userDAO = userDAO;
-
}
-
-
-
public void add(User user) {
-
// TODO Auto-generated method stub
-
userDAO.save(user);
-
}
-
-
}
-
②、修改配置文件apllicationContext.xml
-
-
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
-
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
-
xmlns:cache="http://www.springframework.org/schema/cache"
-
xsi:schemaLocation="
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context.xsd
-
http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx.xsd
-
http://www.springframework.org/schema/jdbc
-
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
-
http://www.springframework.org/schema/cache
-
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop.xsd
-
http://www.springframework.org/schema/util
-
http://www.springframework.org/schema/util/spring-util.xsd">
-
<bean id="u" class="com.dao.UserDAOImpl">
-
</bean>
-
<bean id="userService" class="user.bean.UserServiceImpl">
-
<!-- 构造注入 -->
-
<constructor-arg>
-
<ref bean="u"/>
-
</constructor-arg>
-
</bean>
-
</beans>
③、写测试方法进行测试(略)
3、接口注入
由于接口注入会使类之间形成一定的依赖关系,产生侵入性,所以Spring不支持接口注入,这不是Spring的缺点,而应该是优点。