Spring使用@Required註解依賴檢查

來源:互聯網
上載者:User

標籤:ring   location   comm   actor   執行   this   sch   get   上下文   

Spring依賴檢查 bean 設定檔用於確定的特定類型(基本,集合或對象)的所有屬性被設定。在大多數情況下,你只需要確保特定屬性已經設定但不是所有屬性..對於這種情況,你需要 @Required 註解,請參見下面的例子:@Required樣本Customer對象,適用@Required在 setPerson()方法,以確保 person 屬性已設定。
package com.yiibai.common;import org.springframework.beans.factory.annotation.Required;public class Customer {private Person person;private int type;private String action;public Person getPerson() {return person;}@Requiredpublic void setPerson(Person person) {this.person = person;}}
簡單地套用@Required註解不會強制執行該屬性的檢查,還需要註冊一個RequiredAnnotationBeanPostProcessor以瞭解在bean設定檔@Required註解。RequiredAnnotationBeanPostProcessor可以用兩種方式來啟用。1. 包函 <context:annotation-config />添加 Spring 上下文和 <context:annotation-config />在bean設定檔。
<beans ...xmlns:context="http://www.springframework.org/schema/context"...http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">...<context:annotation-config />...</beans>

完整的執行個體,

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><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>
2. 包函 RequiredAnnotationBeanPostProcessor直接在 bean 設定檔包函“RequiredAnnotationBeanPostProcessor”。
<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 class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/><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>
如果你運行它,下面的錯誤資訊會丟的,因為 person 的屬性未設定。
org.springframework.beans.factory.BeanInitializationException: Property ‘person‘ is required for bean ‘CustomerBean‘
結論嘗試@Required註解,它比依賴檢查XML檔案中更加靈活,因為它可以適用於只有一個特定屬性。定義@Required
請閱讀本文有關如何建立新的自訂 @Required-style 註解。

Spring使用@Required註解依賴檢查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.