java框架篇---spring IOC依赖注入
spring依賴注入的方式有4種
- 構造方法注入
- 屬性注入
- 工廠注入
- 注解注入
下面通過一個實例統一講解:
User.java package com.bjsxt.model;public class User {private String username;private String password;public User(){}public User(String username, String password) {super();this.username = username;this.password = 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;}@Overridepublic String toString() {return "User [username=" + username + ", password=" + password + "]";} } UserDAO.java package com.bjsxt.dao; import com.bjsxt.model.User;public interface UserDAO {public void save(User user); } UserDAO實現類UserDAOImpl package com.bjsxt.dao.impl;import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User;public class UserDAOImpl implements UserDAO {public void save(User user) {//Hibernate//JDBC//XML//NetWorkSystem.out.println("user saved!");}}工廠類
第一個:UserFactory package com.bjsxt.factory;import com.bjsxt.model.User;public class UserFactory {public User CreatUser () {User user=new User();user.setUsername("周1");user.setPassword("oumyye");return user;} } 第二個:UserFactory2 package com.bjsxt.factory;import com.bjsxt.model.User;public class UserFactory2 {public static User CreatUser () {User user=new User();user.setUsername("偶my耶1");user.setPassword("oumyye");return user;} } 控制層:UserService package com.bjsxt.service; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User;public class UserService {private UserDAO userDAO; public void init() {System.out.println("###########init");}public void save(User user) {userDAO.save(user);}public UserDAO getUserDAO() {return userDAO;}public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}public UserService(UserDAO userDAO) {super();this.userDAO = userDAO;}public void destroy() {System.out.println("destroy");} }配置文件:bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 構造方法注入 --><bean id="user" class="com.bjsxt.model.User" ><constructor-arg index="0" type="String" value="zhou"></constructor-arg><constructor-arg index="1" type="String" value="password"></constructor-arg></bean><!-- 屬性注入 --><bean id="user1" class="com.bjsxt.model.User" ><property name="username" value="zhang"></property><property name="password" value="oumyye"></property></bean><!-- 工廠注入 --><bean id= "userFactory" class="com.bjsxt.factory.UserFactory" > </bean><bean id="user2" factory-bean="userFactory" factory-method="CreatUser"></bean><!-- 靜態工廠注入 --><bean id= "user3" class="com.bjsxt.factory.UserFactory2" factory-method="CreatUser"> </bean><bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> <bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype"><!-- <property name="userDAO" ref="u" /> --><!-- 構造方法注入 --><constructor-arg><ref bean="u"/></constructor-arg></bean></beans>測試類:
package com.bjsxt.service;import static org.junit.Assert.*;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.User;public class UserServiceTest {@Testpublic void test() {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");UserService userService=(UserService) applicationContext.getBean("userService");//構造方法注入User user=(User) applicationContext.getBean("user");//屬性注入User user1=(User) applicationContext.getBean("user1");//工廠注入User user2=(User) applicationContext.getBean("user2");//工廠注入User user3=(User) applicationContext.getBean("user3");System.out.println(user);System.out.println(user1);System.out.println(user2);System.out.println(user3);userService.save(user);userService.destroy();}}結果:
###########init User [username=zhou, password=password] User [username=zhang, password=oumyye] User [username=周1, password=oumyye] User [username=偶my耶1, password=oumyye] user saved! destroy注解方式在后面詳細介紹
依賴注入—自動裝配
Spring中提供了自動裝配依賴對象的機制,但是在實際應用中并不推薦使用自動裝配,因為自動裝配會產生未知情況,開發人員無法預見最終的裝配結果。
自動裝配是在配置文件中實現的,如下:
<bean?id="***"?class="***"?autowire="byType">
只需要配置一個autowire屬性即可完成自動裝配,不用再配置文件中寫<property>,但是在類中還是要生成依賴對象的setter方法。
Autowire的屬性值有如下幾個:
·?byType?按類型裝配??可以根據屬性類型,在容器中尋找該類型匹配的bean,如有多個,則會拋出異常,如果沒有找到,則屬性值為null;
·?byName?按名稱裝配??可以根據屬性的名稱在容器中查詢與該屬性名稱相同的bean,如果沒有找到,則屬性值為null;
·?constructor?與byType方式相似,不同之處在與它應用于構造器參數,如果在容器中沒有找到與構造器參數類型一致的bean,那么將拋出異常;
·?autodetect?通過bean類的自省機制(introspection)來決定是使用constructor還是byType的方式進行自動裝配。如果發現默認的構造器,那么將使用byType的方式。
總結
以上是生活随笔為你收集整理的java框架篇---spring IOC依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zookeeper系列五:Master选
- 下一篇: 死在“风口上的猪”人工智能初创企业倒闭的