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

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

我們擁有十載網(wǎng)頁設(shè)計和網(wǎng)站建設(shè)經(jīng)驗,從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計師為您提供的解決方案。為企業(yè)提供網(wǎng)站設(shè)計制作、成都做網(wǎng)站、微信開發(fā)、小程序開發(fā)、手機網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計或者設(shè)計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計服務(wù)并滿足您的需求。

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

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

1、Setter方法注入

Setter方法注入是容器通過調(diào)用無參構(gòu)造器或無參static工廠 方法實例化bean之后,調(diào)用該bean的setter方法,即實現(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的全限定名進行命名。
         eg:
         com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個計數(shù)器的形式,
         用來區(qū)分相同類型的其他bean。
         使用自動化命名很方便,但是沒有多少實際用處,還是建議自己給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)造器來實現(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的全限定名進行命名。
         eg:
         com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個計數(shù)器的形式,
         用來區(qū)分相同類型的其他bean。
         使用自動化命名很方便,但是沒有多少實際用處,還是建議自己給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é)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

文章標(biāo)題:java中springioc依賴注入的方式有哪些
文章分享:http://muchs.cn/article44/piscee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)做網(wǎng)站、面包屑導(dǎo)航企業(yè)網(wǎng)站制作、營銷型網(wǎng)站建設(shè)品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名