當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring依赖检查
生活随笔
收集整理的這篇文章主要介紹了
Spring依赖检查
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Spring中,可以使用依賴檢查功能,以確保所要求的屬性可設置或者注入。 依賴檢查模式 4個依賴檢查支持的模式:
- none –?沒有依賴檢查,這是默認的模式。
- simple –?如果基本類型(int,?long,double…)和集合類型(map,?list..)的任何屬性都沒有設置,UnsatisfiedDependencyException將被拋出。
- objects –?如果對象類型的任何屬性都沒有設置,UnsatisfiedDependencyException將被拋出。
- all –?如果任何類型的任何屬性都沒有被設置,UnsatisfiedDependencyException將被拋出。
注:默認模式是 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> 如果沒有明確定義的依賴檢查模式,其默認為“none”。沒有依賴檢查將執行。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”屬性(基本類型或集合類型),如尚未設置,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'屬性(對象類型),尚未設置,一個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”模式的組合,如果有的話類型(原型,集合和對象)的任何屬性都沒有設置,一個UnsatisfiedDependencyException將被拋出。 全局默認的依賴檢查 明確定義的依賴檢查模式,每個Bean配置繁瑣且容易出錯,可以在<beans>根元素設置一個默認的依賴性檢查屬性強制<beans>根元素內聲明的整個bean類適用此規則。然而,這根默認模式將通過一個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 注解在大多數情況下,你只需要確保特定屬性已經設置,但有一定是所有的類型(原始,集合或對象)屬性。總結
以上是生活随笔為你收集整理的Spring依赖检查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黑旋风李逵(说一说黑旋风李逵的简介)
- 下一篇: 蜂蜜百香果的功效与作用(蜂蜜花粉的功效与