日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring bean依赖注入、bean的装配及相关注解

發布時間:2025/4/16 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring bean依赖注入、bean的装配及相关注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

依賴注入

Spring主要提供以下兩種方法用于依賴注入

  • 基于屬性Setter方法注入
  • 基于構造方法注入

Setter方法注入

例子:

public class Communication {private Messaging messaging;/** DI via Setter*/public void setMessaging(Messaging messaging){this.messaging = messaging;}public void communicate(){messaging.sendMessage();} }

如上Communication類有一個messaging屬性,并含有setMessaging方法,那么使用Setter方法注入的時候,只需要使用如下XML配置即可:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="activeMqMessaging" class="com.websystique.spring.domain.impl.ActiveMQMessaging" /><bean id="communication" class="com.websystique.spring.Communication"><property name="messaging"><ref bean="activeMqMessaging" /></property></bean></beans>

這里省略了ActiveMQMessaging的定義,實際上ActiveMQMessaging類是Messaging接口的一個實現類。

構造方法注入

例子

public class Communication {private Encryption encryption;/** DI via Constructor Injection*/public Communication(Encryption encryption){this.encryption = encryption;}public void communicate(){encryption.encryptData();}}

注意以上Communication類有一個構造方法Communication(Encryption encryption),且含有一個入參,類型為Encryption,那么使用構造方法注入的時候,XML配置如下:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="rsaEncryption" class="com.websystique.spring.domain.impl.RSAEncryption" /><bean id="communication" class="com.websystique.spring.Communication"><constructor-arg type="com.websystique.spring.domain.Encryption"><ref bean="rsaEncryption" /></constructor-arg></bean></beans>

注意,這里省略了RSAEncryption的定義,不用在意這些細節,該類是Encryption接口的一個實現類。

另外,為了避免構造方法重載帶來的歧義,這里指定了入參類型為com.websystique.spring.domain.Encryption。

裝配

bean的裝配有兩種方式,手動裝配和自動裝配。注意,不要混淆,bean的裝配是依賴注入的具體行為,依賴注入的時候需要根據bean的名稱或類型等進行裝配。

手動裝配:通過在<property> 或者 <constructor>標簽中使用ref屬性,在上一小節的“依賴注入”部分使用的就是手動裝配;

<!-- default example (autowire="no") --> <bean id="driver" class="com.websystique.spring.domain.Driver"><property name="license" ref="license"/> </bean><bean id="license" class="com.websystique.spring.domain.License" ><property name="number" value="123456ABCD"/> </bean>

自動裝配:在<bean>標簽中使用autowire屬性;

<bean id="application" class="com.websystique.spring.domain.Application" autowire="byName"/>

本小節主要關注自動裝配,自動裝配有以下四種方式:

  • autowire="byName"?: 根據名稱
  • autowire="byType"?: 根據類型
  • autowire="constructor"?: 根據構造方法入參類型
  • autowire="no"?: 不使用自動裝配,即默認方式,手動裝配

autowire="byName"

例子:

public class Application {private ApplicationUser applicationUser;public ApplicationUser getApplicationUser() {return applicationUser;}public void setApplicationUser(ApplicationUser applicationUser) {this.applicationUser = applicationUser;}@Overridepublic String toString() {return "Application [applicationUser=" + applicationUser + "]";} }

該類有一個屬性叫applicationUser,那么根據名稱自動裝配的XML配置如下:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- byName example --><bean id="application" class="com.websystique.spring.domain.Application" autowire="byName"/><bean id="applicationUser" class="com.websystique.spring.domain.ApplicationUser" ><property name="name" value="superUser"/></bean>
</beans>

autowire="byType"

例子

public class Employee {private EmployeeAddress address;public EmployeeAddress getAddress() {return address;}public void setAddress(EmployeeAddress address) {this.address = address;}@Overridepublic String toString() {return "Employee [address=" + address + "]";} }

該類有一個屬性類型為EmployeeAddress,那么根據類型自動裝配的XML配置如下:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- byType example --><bean id="employee" class="com.websystique.spring.domain.Employee" autowire="byType"/><bean id="employeeAddress" class="com.websystique.spring.domain.EmployeeAddress" ><property name="street" value="112/223,SantaVila"/><property name="city" value="Nebraska"/></bean></beans>

autowire="constructor"

例子

public class Performer {private Instrument instrument;public Performer(Instrument instrument){this.instrument = instrument;}@Overridepublic String toString() {return "Performer [instrument=" + instrument + "]";} }

該類有一個構造方法,入參的類型為Instrument,那么根據構造方法自動裝配的XML配置如下:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- constructor example --><bean id="performer" class="com.websystique.spring.domain.Performer" autowire="constructor"/><bean id="instrument" class="com.websystique.spring.domain.Instrument" ><property name="name" value="PIANO"/></bean></beans>

autowire="no"

public class Driver {private License license;public void setLicense(License license) {this.license = license;}public License getLicense() {return license;}@Overridepublic String toString() {return "Driver [license=" + license + "]";} }

該類有一個屬性license,由于我們不打算使用自動裝配功能,那么只能使用手動裝配了,XML配置如下:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- default example (autowire="no") --><bean id="driver" class="com.websystique.spring.domain.Driver" autowire="no"><property name="license" ref="license"/></bean><bean id="license" class="com.websystique.spring.domain.License" ><property name="number" value="123456ABCD"/></bean></beans>

注意,如果不配置license的ref引用的話,license將為null。

相關注解

主要涉及以下三個注解

  • @Autowired
  • @Resource
  • @Qualifier

@Autowired可應用于構造方法、屬性、setter方法或配置類@Configuration的方法上,該注解根據bean的數據類型進行裝配,如果你想希望根據bean的名稱進行裝配可以使用帶name屬性的@Resource注解;另外@Qualifier注解經常與@Autowired注解結合使用,用于解決一個應用中存在多個同種類型的bean的情況,下面將給出各個注解的示例。

@Autowired(根據類型自動裝配)

setter方法上

@Component("driver") public class Driver {private License license;@Autowiredpublic void setLicense(License license) {this.license = license;}@Overridepublic String toString() {return "Driver [license=" + license + "]";}//getter }

構造方法上

@Component("driver") public class Driver {private License license;@Autowiredpublic Driver(License license){this.license = license;}@Overridepublic String toString() {return "Driver [license=" + license + "]";} }

屬性上

@Component("driver") public class Driver {@Autowiredprivate License license;//getter,setter @Overridepublic String toString() {return "Driver [license=" + license + "]";} }

@Resource(根據名稱裝配)

@Component("application") public class Application {@Resource(name="applicationUser")private ApplicationUser user;@Overridepublic String toString() {return "Application [user=" + user + "]";} }

@Qualifier(與@Autowired結合使用,實現按名稱裝配)

例子背景::存在兩個Car接口的實現類,其中一個Car接口的實現類已被注冊為bean,且name為Mustang

@Component public class Bond {@Autowired@Qualifier("Mustang")private Car car;public void showCar(){car.getCarName();} }

注意,以上例子如果不使用@Qualifier限定的話,將拋出如下異常,表明存在多個類型相同的bean:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.websystique.spring.domain.Car] is defined: expected single matching bean but found 2: Ferari,Mustang
??? at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:970)
??? at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
??? at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
??? ... 14 more

最后提醒下,被@Autowired注解標注默認情況下能保證成功注入,如果注入不成功(往往是找不到,或存在歧義),Spring會拋出異常。當然,有時候可能會有特殊需求,不希望bean被強制裝配,那么可以在@Autowired上添加required=false屬性,表明該bean的裝配是可選的,找不到的話,就為null吧,如下示例:

@Component("driver") public class Driver {@Autowired(required=false)private License license;//getter,setter @Overridepublic String toString() {return "Driver [license=" + license + "]";} }

基于以上原因,雖然@Autowired注解與@Resource功能類似,但是@Autowired還是比@Resource強大了那么一點點,個人建議使用@Autowired注解。


本文轉自風一樣的碼農博客園博客,原文鏈接:http://www.cnblogs.com/chenpi/p/6222595.html,如需轉載請自行聯系原作者

總結

以上是生活随笔為你收集整理的Spring bean依赖注入、bean的装配及相关注解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲干干干 | 亚洲一区二区三区精品视频 | 浪漫樱花动漫在线观看免费 | 鲁丝一区二区三区 | 日韩性生活大片 | 吻胸摸激情床激烈视频大胸 | 极品少妇xxx | jizzjizz中国精品麻豆 | 国产 一二三四五六 | 四虎新网址 | 十大黄台在线观看 | 天天干天天干天天干 | 中国成人毛片 | 337p粉嫩日本欧洲亚洲大胆 | 国产午夜在线一区二区三区 | 中文字幕国产专区 | 国产女女做受ⅹxx高潮 | 青青草欧美 | 久草欧美视频 | 欧美性xxxxx极品娇小 | 国产十八熟妇av成人一区 | 一区精品二区国产 | 亚洲另类图区 | 国产999精品视频 | 久久精品视频一区二区三区 | 久久人妻少妇嫩草av无码专区 | 亚洲一区二区美女 | 动漫一区二区 | 九九自拍偷拍 | 婷婷影音| 特级西西444www高清大胆 | 亚洲色图欧洲色图 | 亚洲欧美电影 | 中文字幕――色哟哟 | av2014天堂| 欧美激情va永久在线播放 | 国产草草浮力影院 | 国产做爰xxxⅹ久久久精华液 | 在线中文字幕视频 | 亚洲风情第一页 | 91香蕉视频黄 | 国产无码精品合集 | 久久水蜜桃 | 国产精品免费久久久 | 张柏芝54张无删码视频 | 久久99热这里只有精品 | 国产精品久久久久久免费免熟 | 最污网站在线观看 | 国产又粗又猛视频免费 | 日韩簧片在线观看 | 人人精品久久 | www一区| 一级黄色大片网站 | 国产精品久久久久久久久久久免费看 | 生活片毛片 | 91干干| 亚洲AV无码成人精品区先锋 | 婷婷伊人 | 婷婷黄色网 | 超污网站在线观看 | 国产日b视频 | 99热免费观看 | 人妻互换 综合 | 精品无码免费视频 | 人人射av | 深夜福利亚洲 | 亚洲av无码一区二区乱子伦as | 国产在线色站 | 国产精品高潮呻吟久久av黑人 | 秘密基地免费观看完整版中文 | 成人免费在线播放视频 | av噜噜| 亚洲成人tv | 男生女生插插插 | 天美视频在线观看 | 日韩一级免费毛片 | caoporm超碰| 中文字幕精品一区 | 中日韩精品在线 | 日韩精品中文在线 | 五月少妇 | 性色av蜜臀av浪潮av老女人 | 久久亚洲一区二区三区四区五区 | 日韩乱码人妻无码中文字幕久久 | 第色 | 欧美成人综合视频 | 99热这里有 | 波多野结衣国产 | 牛牛av | 日韩一级片一区二区 | 久久久久女教师免费一区 | 亚洲美女偷拍 | 综合黄色 | 亚洲好看站 | 国产精品情侣自拍 | 精品国产乱码久久久久久影片 | www黄在线观看 | 后进极品美女白嫩翘臀 | 91快射 |