04-spring的依赖注入
目錄
- 一、spring 中的依賴注入
- 二、構造函數注入
- 1.實現類的構造函數 AccountServiceImpl
- 2.bean.xml
- 三、set 方法注入(更常用)
- 1.實現類 AccountServiceImpl2
- 2. bean.xml
- 四、復雜類型的注入/集合類型的注入
- 1.AccountServiceImpl3
- 2.bean.xml
一、spring 中的依賴注入
- Dependency Injection
- 降低程序間的耦合(依賴關系)
- 以后都交給spring來維護
- 在當前類需要用到其他類的對象,由spring為我們提供,我們只需要在配置文件中說明
- 被稱之為依賴注入
- 能注入的數據:有三類
- 基本類型和 String
- 其他 bean 類型(在配置文件中或者注解配置過的bean)
- 復雜類型/集合類型
二、構造函數注入
一般不用
適應的標簽:constructor-arg
標簽出現的位置:bean標簽的內部
標簽中的屬性:
? type: 用于指定要注入的數據的數據類型,該數據類型也是構造函數中某個或某些參數的類型。
? index: 用于指定要注入的數據給定構造函數中指定索引位置的參數賦值。參數索引的位置從零開始。
? name:用于指定給構造參數中指定名稱的參數賦值。
===============================以上三個用于指定給構造函數中那個參數賦值==========================
? value: 用于提供基本類型和String類型的數據
? ref: 用于指定其他的 bean 類型數據。它指的就是在 spring 中 ioc 核心容器中出現過的 bean 對象
優勢:
? 在獲取 bean 對象時,注入數據是必須的操作,否則對象無法創建成功。
弊端:
? 改變了 bean 對象的實例化方式,使我們在創建對象時,如果用不到這些數據,也必須提供
1.實現類的構造函數 AccountServiceImpl
public class AccountServiceImpl implements IAccountService {//如果是經常變化的數據,并不適用于注入的方式private String name;private Integer age;private Date birthday;public AccountServiceImpl(String name, Integer age, Date birthday) {this.name = name;this.age = age;this.birthday = birthday;}public void saveChange() {System.out.println("保存了"+name+","+age+","+birthday);} }2.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="accountService" class="com.service.impl.AccountServiceImpl"><constructor-arg type="java.lang.String" name="name" value="test"/><constructor-arg name="age" value="18"/><constructor-arg name="birthday" ref="now"/></bean><!--配置一個日期對象--><bean id="now" class="java.util.Date"/> </beans>三、set 方法注入(更常用)
set 方法更加常用
涉及的標簽:property
出現的位置:bean 標簽的內部
標簽的屬性:
? name:用于指定給構造參數中指定名稱的參數賦值。
? 注意:這里的name并不是屬性的名稱,而是 setName 方法中的 Name
? value: 用于提供基本類型和String類型的數據
? ref: 用于指定其他的 bean 類型數據。它指的就是在 spring 中 ioc 核心容器中出現過的 bean 對象
優勢:
? 創建對象時沒有明確的限制,可以使用默認構造函數
弊端:
? 如果有某個成員必須有值,則獲取對象有可能 set 方法沒有執行
1.實現類 AccountServiceImpl2
public class AccountServiceImpl2 implements IAccountService {private String name;private Integer age;private Date birthday;public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}public void setBirthday(Date birthday) {this.birthday = birthday;}public void saveChange() {System.out.println("保存了"+name+","+age+","+birthday);} }2. bean.xml
<bean id="accountService2" class="com.service.impl.AccountServiceImpl2"><property name="name" value="卡茲克"/><property name="age" value="19"/><property name="birthday" ref="now"/></bean>四、復雜類型的注入/集合類型的注入
用于給 List 結構集合注入的標簽:
- ·List Array set
- Map props
1.AccountServiceImpl3
public class AccountServiceImpl3 implements IAccountService {private String[] myStrings;private List<String> myList;private Set<String> mySet;private Map<String,String> myMap;private Properties myProps;public void setMyStrings(String[] myStrings) {this.myStrings = myStrings;}public void setMyList(List<String> myList) {this.myList = myList;}public void setMySet(Set<String> mySet) {this.mySet = mySet;}public void setMyMap(Map<String, String> myMap) {this.myMap = myMap;}public void setMyProps(Properties myProps) {this.myProps = myProps;}public void saveChange() {System.out.println(Arrays.toString(myStrings));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);} }2.bean.xml
<bean id="accountService2" class="com.service.impl.AccountServiceImpl2"><property name="name" value="卡茲克"/><property name="age" value="19"/><property name="birthday" ref="now"/></bean><bean id="accountService3" class="com.service.impl.AccountServiceImpl3"><property name="myStrings"><array><value>aaa</value><value>bbb</value><value>ccc</value></array></property><property name="myList"><list><value>aaa</value><value>bbb</value><value>ccc</value></list></property><property name="mySet"><set><value>aaa</value><value>bbb</value><value>ccc</value></set></property><property name="myMap"><map><entry key="testA" value="aaa"/><entry key="testB" value="bbb"/><entry key="testC"><value>ccc</value></entry></map></property><property name="myProps"><props><prop key="testA">aaa</prop><prop key="testB">bbb</prop></props></property></bean>轉載于:https://www.cnblogs.com/zuiren/p/11415417.html
總結
以上是生活随笔為你收集整理的04-spring的依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 02-耦合和解耦
- 下一篇: 03-spring bean