詳解Spring 兩種注入的方式(Set和構(gòu)造)實(shí)例
依賴注入是指對象之間關(guān)系的控制權(quán)由應(yīng)用代碼中轉(zhuǎn)到外部容器。Spring框架主要提供了Set注入和構(gòu)造注入兩種依賴注入方式。
1:Set注入指的就是在接受注入的類中定義一個要被注入的類型的一個set方法,并在參數(shù)中定義需要注入的元素。Set注入式一種裝配Bean屬性的直接方法,但Set注入的一個缺點(diǎn)就是它假設(shè)了所有的可變屬性都可以通過set方法訪問到,無法清晰地表示哪些屬性是必須的,哪些屬性是可選的。
2:構(gòu)造注入是在接收注入的類中定義一個構(gòu)造方法,并在構(gòu)造方法中定義需要注入的參數(shù)。構(gòu)造注入方式的優(yōu)勢是通過構(gòu)造方法來強(qiáng)制依賴關(guān)系。
下面介紹一下兩種方式的用法:
一:在Myeclipse中(筆者使用的是Myeclipse10版本)新建一個項(xiàng)目(Java project或者web project都可)
二:右鍵項(xiàng)目 - MyEclipse - Add Spring Capabilities(添加Spring支持),選擇3.0版本并添加引用核心類庫,點(diǎn)擊下一步。
三:選擇新創(chuàng)建一個spring bean 配置文件放到項(xiàng)目src目錄下。
四:點(diǎn)擊下一步,指定hibernate 配置頁,直接默認(rèn),點(diǎn)擊完成即可。
五:新建類 HelloWorld.Java
package com.xiami.spring;
public class HelloWorld {
private String str;
/**
* 默認(rèn)構(gòu)造方法
*/
public HelloWorld() {
}
/**
* 用來進(jìn)行構(gòu)造注入的構(gòu)造方法
*
* @param str
*/
public HelloWorld(String str) {
this.str = str;
}
/**
* 用來進(jìn)行Set注入的方法
* @param str
*/
public void setStr(String str) {
this.str = str;
}
/**
* 輸出字符串的方法
*/
public void sayHello() {
System.out.println(str);
}
}
六:新建測試類Test.java
package com.xiami.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//載入spring配置文件
BeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
HelloWorld helloWorld = (HelloWorld) bFactory.getBean("helloService");
helloWorld.sayHello();
}
}
七:打開applicationContext.xml文件,進(jìn)行添加bean配置
以下是兩種方式添加bean:
1:采用Set注入方式的Bean類的配置
右擊applicationContext.xml的編輯界面 - Spring - new bean 打開Bean向?qū)Т翱?,填寫B(tài)ean Id(自定義命名和Test.java中g(shù)etBean("???")對應(yīng)。Bean class 選擇要注入的HelloWorld類。點(diǎn)擊Properties屬性選項(xiàng)卡,給該bean新建一個屬性。
八:在屬性向?qū)Т翱谔顚?Name 對應(yīng)HelloWorld.java中的屬性名稱,Spring Type 選擇value,type選擇String,Value 隨便填值。finish 既可。
九:保存applicationContext.xml, 添加bean后,配置文件多了紅色標(biāo)記的部分,讀者可以運(yùn)行Test.java。測試一下。發(fā)現(xiàn)hello world字符串已經(jīng)注入到了str變量上。
<?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-3.0.xsd">
<!-- 使用Set方式注入 -->
<!--
<span style="color:#ff0000;"><bean id="helloService" class="com.xiami.spring.HelloWorld"
abstract="false" lazy-init="default" autowire="default">
<property name="str">
<value type="java.lang.String">hello world</value>
</property>
</bean></span>
-->
<!-- 使用構(gòu)造方法方式注入
<bean id="helloService" class="com.xiami.spring.HelloWorld"
abstract="false" lazy-init="default" autowire="default">
<constructor-arg>
<value type="java.lang.String">構(gòu)造方法注入方式</value>
</constructor-arg>
</bean>
-->
</beans>
2:采用構(gòu)造注入方式的Bean類的配置
在以上Set方式的Bean Wizard(Bean 向?qū)В┐翱冢贿x擇Properties選項(xiàng)卡,變?yōu)镃onstructor Args選項(xiàng)卡。并Add 新增一個構(gòu)造參數(shù)。Index和Java Class 不用填寫。
十:在增加構(gòu)造方式bean的時候,之前第一個得先注釋或者刪除,不允許有多個id相同的bean。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一文講透為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)
這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Java實(shí)現(xiàn)多個文檔合并輸出到一個文檔
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多個文檔合并輸出到一個文檔的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝的處理方法
這篇文章主要介紹了SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝,使用@RestControllerAdvice對響應(yīng)進(jìn)行增強(qiáng),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
詳解Spring AOP 實(shí)現(xiàn)“切面式”valid校驗(yàn)
本篇文章主要介紹了詳解Spring AOP 實(shí)現(xiàn)“切面式”valid校驗(yàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分
這篇文章主要介紹了mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分頁,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
idea中maven本地倉庫jar包打包失敗和無法引用的問題解決
本文主要介紹了idea中maven本地倉庫jar包打包失敗和無法引用的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

