依賴注入(DI)的三種實現方式
阿新 • • 發佈:2018-12-25
本文來探討一下依賴注入的三種實現方式是哪三種,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 { @Override 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方法 @Override 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;
}
@Override
public void add(User user) {
// TODO Auto-generated method stub
userDAO.save(user);
}
}
②、修改配置檔案apllicationContext.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">
<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的缺點,而應該是優點。