spring—依賴注入

依賴注入(Dependency Injection)

它是 Spring 框架核心 IOC 的具體實現。 在編寫程序時,通過控制反轉,把對象的創建交給了 Spring,但是代碼中不可能出現沒有依賴的情況。
IOC 解耦只是降低他們的依賴關系,但不會消除。例如:業務層仍會調用持久層的方法。 那這種業務層和持久層的依賴關系,在使用 Spring 之后,就讓 Spring 來維護了。
簡單的說,就是坐等框架把持久層對象傳入業務層,而不用我們自己去獲取

Bean的依賴注入方式

①構造方法
創建有參構造

public class service {Dao dao;public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}```java
<?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 id="impl" class="com.ImplDao" /><bean id="service" class="com.service"><constructor-arg name="dao" ref="impl"/></bean>
</beans>

②set方法

<?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 id="impl" class="com.ImplDao" /><bean id="service" class="com.service"><property name="dao" ref="impl"/></bean>
</beans>
public class service {Dao dao;public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();}public void setDao(Dao dao) {this.dao = dao;}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}

set方法:P命名空間注入
P命名空間注入本質也是set方法注入,但比起上述的set方法注入更加方便,主要體現在配置文件中,如下: 首先,需要引入P命名空間:

  xmlns:p="http://www.springframework.org/schema/p"
 <bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"></bean>
</beans>

Bean的依賴注入的數據類型

(1)普通數據類型的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/></bean>
</beans>
public class service {Dao dao;int no;String  name;public void setNo(int no) {this.no = no;}public void setName(String name) {this.name = name;}public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();System.out.println(this.toString());}public void setDao(Dao dao) {this.dao = dao;}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}

(2)集合數據類型(List)的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property></bean>
</beans>

(4)集合數據類型( Map<String,ImplDao> )的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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="impl" class="com.ImplDao" /><bean id="impl2" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property><property name="map"><map><entry key="i1" value-ref="impl"/><entry key="i2" value-ref="impl2"/></map></property></bean>
</beans>

(5)集合數據類型(Properties)的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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="impl" class="com.ImplDao" /><bean id="impl2" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property><property name="map"><map><entry key="i1" value-ref="impl"/><entry key="i2" value-ref="impl2"/></map></property><property name="properties"><props><prop key="aa">11</prop><prop key="bb">22</prop></props></property></bean>
</beans>

引入其他配置文件(分模塊開發)

實際開發中,Spring的配置內容非常多,這就導致Spring配置很繁雜且體積很大,所以,可以將部分配置拆解到其 他配置文件中,而在Spring主配置文件通過import標簽進行加載

    <import resource="applicationContext-dao.xml" />

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391966.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391966.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391966.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C# (類型、對象、線程棧和托管堆)在運行時的相互關系

在介紹運行時的關系之前,先從一些計算機基礎只是入手,如下圖: 該圖展示了已加載CLR的一個windows進程,該進程可能有多個線程,線程創建時會分配到1MB的棧空間.棧空間用于向方法傳遞實參,方法定義的局部變量也在實參上,上圖的右側展示了線程的棧內存,棧從高位內存地址向地位內存地…

2019-08-01 紀中NOIP模擬賽B組

T1 [JZOJ2642] 游戲 題目描述 Alice和Bob在玩一個游戲&#xff0c;游戲是在一個N*N的矩陣上進行的&#xff0c;每個格子上都有一個正整數。當輪到Alice/Bob時&#xff0c;他/她可以選擇最后一列或最后一行&#xff0c;并將其刪除&#xff0c;但必須保證選擇的這一行或這一列所有…

knn分類 knn_關于KNN的快速小課程

knn分類 knnAs the title says, here is a quick little lesson on how to construct a simple KNN model in SciKit-Learn. I will be using this dataset. It contains information on students’ academic performance.就像標題中所說的&#xff0c;這是關于如何在SciKit-Le…

spring—配置數據源

數據源&#xff08;連接池&#xff09;的作用 數據源(連接池)是提高程序性能如出現的 事先實例化數據源&#xff0c;初始化部分連接資源 使用連接資源時從數據源中獲取 使用完畢后將連接資源歸還給數據源 常見的數據源(連接池)&#xff1a;DBCP、C3P0、BoneCP、Druid等 開發步…

大型網站系統與Java中間件實踐pdf

下載地址&#xff1a;網盤下載 基本介紹 編輯內容簡介 到底是本什么書&#xff0c;擁有這樣一份作序推薦人列表&#xff1a;阿里集團章文嵩博士|新浪TimYang|去哪網吳永強|丁香園馮大輝|蘑菇街岳旭強|途牛湯崢嶸|豆瓣洪強寧|某電商陳皓/林昊…… 這本書出自某電商技術部總監之手…

office漏洞利用--獲取shell

環境&#xff1a; kali系統&#xff0c; windows系統 流程&#xff1a; 在kali系統生成利用文件&#xff0c; kali系統下監聽本地端口&#xff0c; windows系統打開doc文件&#xff0c;即可中招 第一種利用方式&#xff0c; 適合測試用&#xff1a; 從git下載代碼&#xff1a; …

pandas之DataFrame合并merge

一、merge merge操作實現兩個DataFrame之間的合并&#xff0c;類似于sql兩個表之間的關聯查詢。merge的使用方法及參數解釋如下&#xff1a; pd.merge(left, right, onNone, howinner, left_onNone, right_onNone, left_indexFalse, right_indexFalse,    sortFalse, suffi…

typescript_如何掌握高級TypeScript模式

typescriptby Pierre-Antoine Mills皮埃爾安托萬米爾斯(Pierre-Antoine Mills) 如何掌握高級TypeScript模式 (How to master advanced TypeScript patterns) 了解如何為咖喱和Ramda創建類型 (Learn how to create types for curry and Ramda) Despite the popularity of curry…

html函數splice,js數組的常用函數(slice()和splice())和js引用的三種方法總結—2019年1月16日...

總結&#xff1a;slice()和splice()slice(參數1,參數2)可以查找數組下對應的數據&#xff0c;參數1為起始位置&#xff0c;參數2為結束位置&#xff0c;參數2可以為負數&#xff0c;-1對應的是從后向前數的第一個數值。splice()可以進行增刪改查數據操作&#xff0c;splice(參數…

leetcode 643. 子數組最大平均數 I(滑動窗口)

給定 n 個整數&#xff0c;找出平均數最大且長度為 k 的連續子數組&#xff0c;并輸出該最大平均數。 示例&#xff1a; 輸入&#xff1a;[1,12,-5,-6,50,3], k 4 輸出&#xff1a;12.75 解釋&#xff1a;最大平均數 (12-5-650)/4 51/4 12.75 代碼 class Solution {publ…

python ==字符串

字符串類型(str)&#xff1a; 包含在引號&#xff08;單&#xff0c;雙&#xff0c;三&#xff09;里面&#xff0c;由一串字符組成。 用途&#xff1a;姓名&#xff0c;性別&#xff0c;地址&#xff0c;學歷&#xff0c;密碼 Name ‘zbk’ 取值: 首先要明確&#xff0c;字符…

認證鑒權與API權限控制在微服務架構中的設計與實現(一)

作者&#xff1a; [Aoho’s Blog] 引言&#xff1a; 本文系《認證鑒權與API權限控制在微服務架構中的設計與實現》系列的第一篇&#xff0c;本系列預計四篇文章講解微服務下的認證鑒權與API權限控制的實現。 1. 背景 最近在做權限相關服務的開發&#xff0c;在系統微服務化后&a…

mac下完全卸載程序的方法

在國外網上看到的&#xff0c;覺得很好&#xff0c;不僅可以長卸載的知識&#xff0c;還對mac系統有更深的認識。比如偏好設置文件&#xff0c;我以前設置一個程序壞了&#xff0c;打不開了&#xff0c;怎么重裝都打不開&#xff0c;后來才知道系統還保留著原來的偏好設置文件。…

機器學習集群_機器學習中的多合一集群技術在無監督學習中應該了解

機器學習集群Clustering algorithms are a powerful technique for machine learning on unsupervised data. The most common algorithms in machine learning are hierarchical clustering and K-Means clustering. These two algorithms are incredibly powerful when appli…

自考本科計算機要學什么,計算機自考本科需要考哪些科目

高科技發展時代&#xff0c;怎離得開計算機技術&#xff1f;小學生都要學編程了&#xff0c;未來趨勢一目了然&#xff0c;所以如今在考慮提升學歷的社會成人&#xff0c;多半也青睞于計算機專業&#xff0c;那么計算機自考本科需要考哪些科目&#xff1f;難不難&#xff1f;自…

審查指南 最新版本_代碼審查-最終指南

審查指南 最新版本by Assaf Elovic通過阿薩夫埃洛維奇 代碼審查-最終指南 (Code Review — The Ultimate Guide) 構建團隊代碼審查流程的終極指南 (The ultimate guide for building your team’s code review process) After conducting hundreds of code reviews, leading R…

非對稱加密

2019獨角獸企業重金招聘Python工程師標準>>> 概念 非對稱加密算法需要兩個密鑰&#xff1a;公鑰&#xff08;publickey&#xff09;和私鑰&#xff08;privatekey&#xff09;。公鑰與私鑰是一對&#xff0c;如果用公鑰對數據進行加密&#xff0c;只有用對應的私…

管理Sass項目文件結構

http://www.w3cplus.com/preprocessor/architecture-sass-project.html 編輯推薦&#xff1a; 掘金是一個高質量的技術社區&#xff0c;從 CSS 到 Vue.js&#xff0c;性能優化到開源類庫&#xff0c;讓你不錯過前端開發的每一個技術干貨。 點擊鏈接查看最新前端內容&#xff0c…

Spring—注解開發

Spring原始注解 Spring是輕代碼而重配置的框架&#xff0c;配置比較繁重&#xff0c;影響開發效率&#xff0c;所以注解開發是一種趨勢&#xff0c;注解代替xml配置文 件可以簡化配置&#xff0c;提高開發效率。 Component 使用在類上用于實例化BeanController 使用在web層類…

政府公開數據可視化_公開演講如何幫助您設計更好的數據可視化

政府公開數據可視化What do good speeches and good data visualisation have in common? More than you may think.好的演講和好的數據可視化有什么共同點&#xff1f; 超出您的想象。 Aristotle — the founding father of all things public speaking — believed that th…