spring中的依赖注入——构造函数注入、set方法注入( 更常用的方式)、复杂类型的注入/集合类型的注入
spring中的依賴注入
依賴注入: Dependency Injection IOC的作用:降低程序間的耦合(依賴關(guān)系) 依賴關(guān)系的管理:以后都交給spring來(lái)維護(hù)。在當(dāng)前類需要用到其他類的對(duì)象,由spring為我們提供,我們只需要在配置文件中說(shuō)明 依賴關(guān)系的維護(hù):就稱之為依賴注入依賴注入:能注入的數(shù)據(jù):有三類 基本類型和String其他bean類型(在配置文件中或者注解配置過(guò)的bean)復(fù)雜類型/集合類型注入的方式:有三種 第一種:使用構(gòu)造函數(shù)提供第二種:使用set方法提供第三種:使用注解提供
IAccountService.java
package com.itheima.service;/*** 賬戶業(yè)務(wù)層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;import java.util.Date;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ public class AccountServiceImpl implements IAccountService {//如果是經(jīng)常變化的數(shù)據(jù),并不適用于注入的方式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 saveAccount(){System.out.println("service中的saveAccount方法執(zhí)行了。。。"+name+","+age+","+birthday);}}構(gòu)造函數(shù)注入
?
? ? 使用的標(biāo)簽:constructor-arg
??? 標(biāo)簽出現(xiàn)的位置:bean標(biāo)簽的內(nèi)部
??? 標(biāo)簽中的屬性
??????? type:用于指定要注入的數(shù)據(jù)的數(shù)據(jù)類型,該數(shù)據(jù)類型也是構(gòu)造函數(shù)中某個(gè)或某些參數(shù)的類型
??????? index:用于指定要注入的數(shù)據(jù)給構(gòu)造函數(shù)中指定索引位置的參數(shù)賦值。索引的位置是從0開(kāi)始
??????? name:用于指定給構(gòu)造函數(shù)中指定名稱的參數(shù)賦值??????????????????????????????????????? 常用的
??????? =============以上三個(gè)用于指定給構(gòu)造函數(shù)中哪個(gè)參數(shù)賦值===============================
??????? value:用于提供基本類型和String類型的數(shù)據(jù)
??????? ref:用于指定其他的bean類型數(shù)據(jù)。它指的就是在spring的Ioc核心容器中出現(xiàn)過(guò)的bean對(duì)象
??? 優(yōu)勢(shì):
??????? 在獲取bean對(duì)象時(shí),注入數(shù)據(jù)是必須的操作,否則對(duì)象無(wú)法創(chuàng)建成功。
??? 弊端:
??????? 改變了bean對(duì)象的實(shí)例化方式,使我們?cè)趧?chuàng)建對(duì)象時(shí),如果用不到這些數(shù)據(jù),也必須提供。
Client.java
package com.itheima.ui;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個(gè)表現(xiàn)層,用于調(diào)用業(yè)務(wù)層*/ public class Client {public static void main(String[] args) {//1.獲取核心容器對(duì)象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根據(jù)id獲取Bean對(duì)象IAccountService as = (IAccountService)ac.getBean("accountService");as.saveAccount();} }bean.xml
set方法注入??????????????? 更常用的方式
? ? 涉及的標(biāo)簽:property
??? 出現(xiàn)的位置:bean標(biāo)簽的內(nèi)部
??? 標(biāo)簽的屬性
??????? name:用于指定注入時(shí)所調(diào)用的set方法名稱
??????? value:用于提供基本類型和String類型的數(shù)據(jù)
??????? ref:用于指定其他的bean類型數(shù)據(jù)。它指的就是在spring的Ioc核心容器中出現(xiàn)過(guò)的bean對(duì)象
??? 優(yōu)勢(shì):
??????? 創(chuàng)建對(duì)象時(shí)沒(méi)有明確的限制,可以直接使用默認(rèn)構(gòu)造函數(shù)
??? 弊端:
??????? 如果有某個(gè)成員必須有值,則獲取對(duì)象是有可能set方法沒(méi)有執(zhí)行。
AccountServiceImpl2.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;import java.util.Date;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ public class AccountServiceImpl2 implements IAccountService {//如果是經(jīng)常變化的數(shù)據(jù),并不適用于注入的方式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 saveAccount(){System.out.println("service中的saveAccount方法執(zhí)行了。。。"+name+","+age+","+birthday);}}bean.xml
Client.java
package com.itheima.ui;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個(gè)表現(xiàn)層,用于調(diào)用業(yè)務(wù)層*/ public class Client {public static void main(String[] args) {//1.獲取核心容器對(duì)象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");IAccountService as = (IAccountService)ac.getBean("accountService2");as.saveAccount();} }復(fù)雜類型的注入/集合類型的注入
? ? 用于給List結(jié)構(gòu)集合注入的標(biāo)簽: list array set
???????
??? 用于個(gè)Map結(jié)構(gòu)集合注入的標(biāo)簽:map? props
???????
??? 結(jié)構(gòu)相同,標(biāo)簽可以互換
AccountServiceImpl3.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.Set; import java.util.Map;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ public class AccountServiceImpl3 implements IAccountService {private String[] myStrs;private List<String> myList;private Set<String> mySet;private Map<String,String> myMap;private Properties myProps;public void setMyStrs(String[] myStrs) {this.myStrs = myStrs;}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 saveAccount(){System.out.println(Arrays.toString(myStrs));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);} }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.xsd"><bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3"><property name="myStrs"><set><value>AAA</value><value>BBB</value><value>CCC</value></set></property><property name="myList"><array><value>AAA</value><value>BBB</value><value>CCC</value></array></property><property name="mySet"><list><value>AAA</value><value>BBB</value><value>CCC</value></list></property><property name="myMap"><props><prop key="testC">ccc</prop><prop key="testD">ddd</prop></props></property><property name="myProps"><map><entry key="testA" value="aaa"></entry><entry key="testB"><value>BBB</value></entry></map></property></bean></beans>Client.java
package com.itheima.ui;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個(gè)表現(xiàn)層,用于調(diào)用業(yè)務(wù)層*/ public class Client {public static void main(String[] args) {//1.獲取核心容器對(duì)象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");IAccountService as = (IAccountService)ac.getBean("accountService3");as.saveAccount();} }總結(jié)
以上是生活随笔為你收集整理的spring中的依赖注入——构造函数注入、set方法注入( 更常用的方式)、复杂类型的注入/集合类型的注入的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 把对象的创建交给spring来管理——
- 下一篇: spring基于注解的IOC以及IoC的