溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java中spring ioc依賴注入的方式有哪些

發(fā)布時間:2021-02-05 10:29:51 來源:億速云 閱讀:300 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)java中spring ioc依賴注入的方式有哪些,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

spring ioc注入的三種方式是:1、Setter方法注入,是容器通過調(diào)用無參構(gòu)造器或無參static工廠 方法實(shí)例化bean之后,調(diào)用該bean的setter方法。2、構(gòu)造方法注入。3、P命名空間注入。

Spring IOC(依賴注入的三種方式):

1、Setter方法注入

Setter方法注入是容器通過調(diào)用無參構(gòu)造器或無參static工廠 方法實(shí)例化bean之后,調(diào)用該bean的setter方法,即實(shí)現(xiàn)了基于setter的依賴注入。

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;public class HelloWord {
private HelloService helloService;
 
    // setter方式注入Bean
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloService.sayHello("大家好!");
    }
 
}
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
       Bean聲明:
         該bean類似于javaConfig中的@Bean注解;
         用于創(chuàng)建bean的類通過class屬性來指定,并且需要使用全限定的類名。
         通過id指定bean的ID。如果不顯示指定,默認(rèn)使用class的全限定名進(jìn)行命名。
         eg:
         com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個計數(shù)器的形式,
         用來區(qū)分相同類型的其他bean。
         使用自動化命名很方便,但是沒有多少實(shí)際用處,還是建議自己給bean顯示設(shè)定ID。
     -->
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
 
    <!-- setter注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
        <property name="helloService" ref="helloService"/>
    </bean>
 
</beans>

2、構(gòu)造方法注入

構(gòu)造器依賴注入通過容器觸發(fā)一個類的構(gòu)造器來實(shí)現(xiàn)的,該類有一系列參數(shù),每個參數(shù)代表一個對其他類的依賴。

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;

public class HelloWord {
    private HelloService helloService;
 
    // 構(gòu)造方法注入
    public HelloWord (HelloService helloService) {
        this.helloService = helloService;
    }
 
}
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
       Bean聲明:
         該bean類似于javaConfig中的@Bean注解;
         用于創(chuàng)建bean的類通過class屬性來指定,并且需要使用全限定的類名。
         通過id指定bean的ID。如果不顯示指定,默認(rèn)使用class的全限定名進(jìn)行命名。
         eg:
         com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個計數(shù)器的形式,
         用來區(qū)分相同類型的其他bean。
         使用自動化命名很方便,但是沒有多少實(shí)際用處,還是建議自己給bean顯示設(shè)定ID。
     -->
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
 
    <!-- 構(gòu)造方法注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">
        <constructor-arg><ref bean="helloService"/></constructor-arg>
    </bean>
 
</beans>

3、P命名空間注入

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;

public class HelloWord {
    //名字
    private String name;
    //年齡
    private String age;
    //方法類
    private HelloService helloService;
 
    public void setName (String name) {
        this.name = name;
    }
    
    public void setAge (String age) {
        this.age = age;
    }
    
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloService.sayHello("我叫"+ name + ",今年" + age + "歲,大家好!");
    }
 
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!-- 引入p命名標(biāo)簽 -->
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>
 
    <!-- p標(biāo)簽注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" 
     p:name="明明" p:age="24" p:helloService-ref="helloService"></bean>
 
</beans>

P標(biāo)簽注入集合bean

package com.jpeony.spring.setter;
import com.jpeony.spring.common.HelloServiceImpl;
import java.util.List;

public class HelloWord {
    //名字
    private String name;
    //年齡
    private String age;
    //方法類
    private List<HelloService> helloServices;
 
    public void setName (String name) {
        this.name = name;
    }
    
    public void setAge (String age) {
        this.age = age;
    }
    
    public void setHelloServices(List<HelloService> helloServices) {
        this.helloServices = helloServices;
    }
 
    @Override
    public void selfIntroduction() {
        // 向大家打招呼
        helloServices[0].sayHello("我叫"+ name + ",今年" + age + "歲,大家好!");
    }
 
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!-- 引入p命名標(biāo)簽 -->
       xmlns:p="http://www.springframework.org/schema/p"
       <!-- 引入util命名標(biāo)簽 -->
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>

    <bean id="helloService2" class="com.jpeony.spring.common.HelloServiceImpl">
    ...........
    </bean>
 
    <util:list id="helloServices">
        <ref bean="helloService"/>
        <ref bean="helloService2"/>
    </util:list>

    <!-- p標(biāo)簽注入bean -->
    <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" 
     p:name="明明" p:age="24" p:helloServices-ref="helloServices"></bean>
 
</beans>

關(guān)于“java中spring ioc依賴注入的方式有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI