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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring依赖检查

發(fā)布時間:2024/9/20 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring依赖检查 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在Spring中,可以使用依賴檢查功能,以確保所要求的屬性可設(shè)置或者注入。 依賴檢查模式 4個依賴檢查支持的模式:
  • none –?沒有依賴檢查,這是默認(rèn)的模式。
  • simple –?如果基本類型(int,?long,double…)和集合類型(map,?list..)的任何屬性都沒有設(shè)置,UnsatisfiedDependencyException將被拋出。
  • objects –?如果對象類型的任何屬性都沒有設(shè)置,UnsatisfiedDependencyException將被拋出。
  • all –?如果任何類型的任何屬性都沒有被設(shè)置,UnsatisfiedDependencyException將被拋出。

注:默認(rèn)模式是 none

示例

Customer和Person對象的示例。 package com.yiibai.common;public class Customer {private Person person;private int type;private String action;//getter and setter methods } package com.yiibai.common;public class Person {private String name;private String address;private int age;//getter and setter methods }

1. none?依賴檢查

Spring?bean配置文件使用?“none”?依賴檢查模式。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.common.Customer" ><property name="action" value="buy" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans> 如果沒有明確定義的依賴檢查模式,其默認(rèn)為“none”。沒有依賴檢查將執(zhí)行。

2. simple?依賴檢查

Spring?bean的配置文件使用“simple”依賴檢查模式。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.common.Customer" dependency-check="simple"><property name="person" ref="PersonBean" /><property name="action" value="buy" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans> 在“type”屬性(基本類型或集合類型),如尚未設(shè)置,UnsatisfiedDependencyException將拋出。 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource [config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'type': Set this property value or disable dependency checking for this bean.

3. objects?依賴檢查

Spring?bean配置文件的?“objects”依賴檢查模式。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.common.Customer" dependency-check="objects"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans> 在'person'屬性(對象類型),尚未設(shè)置,一個UnsatisfiedDependencyException將拋出。 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource [config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'person': Set this property value or disable dependency checking for this bean.

4. all?依賴檢查

Spring?bean配置文件的“all”依賴檢查模式。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.common.Customer" dependency-check="all"><property name="action" value="buy" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans> 對“simple”和“objects”模式的組合,如果有的話類型(原型,集合和對象)的任何屬性都沒有設(shè)置,一個UnsatisfiedDependencyException將被拋出。 全局默認(rèn)的依賴檢查 明確定義的依賴檢查模式,每個Bean配置繁瑣且容易出錯,可以在<beans>根元素設(shè)置一個默認(rèn)的依賴性檢查屬性強(qiáng)制<beans>根元素內(nèi)聲明的整個bean類適用此規(guī)則。然而,這根默認(rèn)模式將通過一個bean自己指定的模式下可覆蓋。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-dependency-check="all"><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans> 在這個配置文件中聲明所有的Bean類默都是“all”依賴檢查模式。 @Required 注解在大多數(shù)情況下,你只需要確保特定屬性已經(jīng)設(shè)置,但有一定是所有的類型(原始,集合或?qū)ο?屬性。

總結(jié)

以上是生活随笔為你收集整理的Spring依赖检查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。