1. 程式人生 > >Spring Ioc-依賴注入的幾種方式

Spring Ioc-依賴注入的幾種方式

一 setter方法注入

配置檔案如下:

<bean id="helloAction" class="org.yoo.action.SpringSetterHelloAction">
<!-- setter injection using the nested <ref/> element -->
<property name="helloservice"><ref bean="helloService"/></property>
<!--可以不設定型別 -->
<property name="name" value="yoo"></property>
</bean>

action實現類中程式碼:

private IHelloService helloservice;
private String name ;
public void sayHello(){
helloservice.sayHello();
System.out.println(this.name);
}

public void setHelloservice(IHelloService helloservice) {
this.helloservice = helloservice;
}

public void setName(String name) {
this.name = name;
}

這裡的name和helloservice都採用屬性的setter方法注入。即類中設定一個全域性屬性,並對屬性有setter方法,以供容器注入。

二 構造器注入

spring也支援構造器注入,也即有些資料中的構造子或者建構函式注入。

先看配置檔案:

<!--普通構造器注入-->
<bean id="helloAction" class="org.yoo.action.ConstructorHelloAction">
<!--type 必須為java.lang.String 因為是按型別匹配的,不是按順序匹配-->

<constructor-arg type="java.lang.String" value="yoo"/>
<!-- 也可以使用index來匹配-->
<!--<constructor-arg index="1" value="42"/>-->
<constructor-arg><ref bean="helloService"/></constructor-arg>
</bean>

action實現類中程式碼:

private HelloServiceImpl helloservice;
private String name ;

public SpringConstructorHelloAction(HelloServiceImpl helloservice,String name){
this.helloservice = helloservice;
this.name = name ;
}

@Override
public void sayHello() {
helloservice.sayHello();
System.out.println(this.name);
}

同樣設定2個屬性,然後在建構函式中注入。

三靜態工廠注入

配置檔案如下:

<!--靜態工廠構造注入-->

<!--注意這裡的class 帶factory-method引數-->

<!--factory-method的值是FactoryHelloAction中的工廠方法名createInstance-->
<bean id="helloAction" class="org.yoo.di.FactoryHelloAction" factory-method="createInstance">
<constructor-arg type="java.lang.String" value="yoo"/>
<constructor-arg><ref bean="helloService"/></constructor-arg>
</bean>


action實現類:

private HelloServiceImpl helloservice;
private String name = null;

private SpringFactoryHelloAction(String name ,HelloServiceImpl helloservice){
this.helloservice = helloservice ;
this.name = name ;
}

public static SpringFactoryHelloAction createInstance(String name ,HelloServiceImpl helloservice) {
SpringFactoryHelloAction fha = new SpringFactoryHelloAction (name,helloservice);
// some other operations
return fha;
}


@Override
public void sayHello() {
helloservice.sayHello();
System.out.println(this.name);
}
四 無配置檔案注入(自動注入)

上面三種方法都需要編寫配置檔案,在spring2.5中還提供了不編寫配置檔案的ioc實現。需要注意的是,無配置檔案指bean之間依賴,不基於配置檔案,而不是指沒有spring配置檔案。

配置檔案如下:

<?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-2.5.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="helloService" class="org.yoo.service.HelloServiceImpl">
</bean>
<bean id="helloAction" class="org.yoo.action.AutowiredHelloAction">
</bean>
</beans>

可見上面只是設定了helloService和helloAction兩個bean,並沒有設定helloAction對helloService的依賴。

另外<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>是必須的,有此才支援自動注入。

action實現類:

/*
* 屬性自動裝載
*/
/*
@Autowired
private HelloService helloservice;

@Override
public void sayHello() {
helloservice.sayHello();

上面程式碼很簡單,在屬性上加上 @Autowired註釋,spring會自動注入HelloService 。

同樣也支援構造器和setteer方法的注入,如下:

構造器自動注入:

/*
* 還可以使用建構函式設定
*/
@Autowired
public SpringAutowiredHelloAction(HelloServiceImpl helloservice){
this.helloservice = helloservice;
}

setter方法自動注入:

/*
* 也可以使用set方法設定
*/
/*
@Autowired
public void setHelloservice(HelloService helloservice) {
this.helloservice = helloservice;
}




最後在spring的reference文件中有提到,如果不使用自動注入,儘量使用setter方法,一般通用的也是使用setter方法。


而使用自動注入還是配置檔案方式,如果jdk低於1.5或者spring不是2.5,就只能夠使用配置檔案方式了。其它的就看實際專案選擇了。

相關推薦

Spring Ioc-依賴注入方式

一 setter方法注入 配置檔案如下: <bean id="helloAction" class="org.yoo.action.SpringSetterHelloAction"><!-- setter injection using the nested

Spring 依賴注入方式的實現,及迴圈依賴問題的解決(原始碼+XML配置)

搬磚啦,搬磚啦,這幾天在看Spring相關的書,下面給大家分享一下這幾天的心得與收穫,Go Go Go! Spring支援兩種依賴注入方式,分別是屬性注入,建構函式注入。除此之外,Spring還支援工廠注入方式。 接下來,我們一起來了解一下Spring的幾種注入方式。

Spring 依賴注入方式

紙上得來終覺淺 1.構造器注入 AnimalDao: package com.roadArchitectWeb.dao; public interface AnimalDao { /*所有動物有一個說話的方法*/ public void say(); } CatDao

Spring IOC 依賴注入( 二 )

目錄 1、什麼是IOC 2、什麼是DI 3、第一個IOC示例程式 -- 通過id獲取物件(重點) 1、建立一個Java工程:             

spring依賴注入 -------基於註解方式

前言: 做了2年的軟體,剛開始入行的時候,沒有個目標基本上都是在摸索,技術看的我眼花繚亂,這個想學,那個也想學結果是對很多技術一知半解的,工作中才發現,我們要掌握的一門可以搞定快速開發搞定所有業務需求的技術, 所以現在我要對spring的東西達到一個深層次的掌握,儘量避免百度,在開發

SSM(一):spring-ioc依賴注入筆記

一、java代理模式   java代理模式是ioc的前置知識。代理模式非常簡單,看程式碼就一目瞭然了。 public interface role { public void makeMoney(); } role public cla

Spring IOC 依賴注入( 二 )

目錄 圖解: 流程圖解: 圖解流程: 1、什麼是IOC IOC 全稱指的是 Inverse Of Control 控制反轉。 原

Spring建立物件的方式

     * 1)、包掃描+元件標註註解(@Controller/@Service/@Repository/@Component)[自己寫的類]      * 2)、@Bean[匯入的第三方包裡面的元件] 參考部落格:Spring註解@Conditional-

Spring啟用profile的方式

1、配置web.xml:通過配置context-param指定ContextLoaderListener初始化用到的引數;如下,可啟用test的profile。 <?xml version="1.0" encoding="UTF-8"?> <web-app vers

Spring的事物有方式?談談spring事物的隔離級別和傳播行為?

宣告式事務    使用spring宣告式事務,spring使用AOP來支援宣告式事務,會根據事務屬性,自動在方法呼叫之前決定是否開啟一個事務,並在方法執行之後決定事務提交或回滾事務。 事務的隔離級別: 資料庫系統提供了4種事務隔離級別,在這4種隔離級別中,Serial

Spring事務管理之方式實現事務

1、事務認識 大家所瞭解的事務Transaction,它是一些列嚴密操作動作,要麼都操作完成,要麼都回滾撤銷。Spring事務管理基於底層資料庫本身的事務處理機制。資料庫事務的基礎,是掌握Spring事務管理的基礎。這篇總結下Spring事務。 事務具備ACID四種特性,

Spring整理系列(17)————循序漸進了解spring事務管理的方式

先從例項開始。。。。 一、例項基本業務為銀行轉賬,A賬戶向B賬戶轉賬,業務執行過程要保證A、B兩個帳號資料操作同時成功或失敗,此時就需要事務進行控制,基本例項程式碼如下: 轉賬DAO: public interface AccountDao {

先碼後看 spring配置bean的方式:xml直接配置、靜態工廠、例項工廠、factory bean、註解 侵立刪

轉自:http://blog.csdn.net/love___code/article/details/53167138 spring框架的核心在於“Ioc控制反轉”“DI依賴注入”以及“AOP面向切面程式設計”,所以在實現這些核心是最基礎的一步即為在ioc容器中配置b

面試中被問Spring迴圈依賴的三方式!!!

什麼是迴圈依賴? 迴圈依賴其實就是迴圈引用,也就是兩個或則兩個以上的 Bean 互相持有對方,最終形成閉環。比如A依賴於B,B依賴於C,C又依賴於A。如下圖: 如果在日常開發中我們用new 物件的方式發生這種迴圈依賴的話程式會在執行時一直迴圈呼叫,直至記憶體溢位報錯。下面說一下Spring是如果解決迴

Spring依賴注入(註解方式)、泛型依賴注入

註解方式實現依賴注入支援手工裝配和自動裝配(慎用) 一般是宣告bean和bean直接的依賴關係的時候用比較好 使用註解方式時,也支援給Field注入值(在XML中不可以給Field注入)。另外就是setter方式注入。 @Resource註解在spring安裝目錄的lib\

獲取Spring ROOT ApplicationContext 的方式

在spring中我們經常需要獲取ROOT ApplicationContext, 以下就幾種獲取方式作分析: 在使用spring的web專案中,在web.xml中有如下配置,進行spring的初始化: <listener> &

Android Studio 新增依賴方式

1、庫依賴(library)  2、模組依賴(module)  以上兩種又可以分為內部和外部 a、內部庫  在工程的lib下直接貼上jar、aar檔案,androidstudio會自動載入指定目錄下的依賴庫。  即粘貼後,右鍵add as library。build.g

spring獲取bean的方式

使用jdk:1.8、maven:3.3.3 spring獲取Bean的方式 pom.xml檔案內容: <?xml v

Spring Ioc 之二 -依賴注入方式

一 setter方法注入 上一篇中Spring版HelloWorld中,helloaction注入helloservice是採用了setter方法。 配置檔案如下: action實現類中程式碼: private IHelloService helloservice;

Spring依賴注入方式實現鬆耦合

一、普通注入方式: (1)在IDEA工作空間內先建立lib包然後匯入Spring的架包(複製進去的架包要按住滑鼠右鍵出現Add as Library)。 (2)在已經建立好架包的基礎上在src目錄下建立XML檔案,檔案命為applicationContext.xml,需要注意的是我們建