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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring使用@Required注解依赖检查

發(fā)布時間:2024/9/20 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring使用@Required注解依赖检查 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Spring依賴檢查?bean 配置文件用于確定的特定類型(基本,集合或?qū)ο?的所有屬性被設(shè)置。在大多數(shù)情況下,你只需要確保特定屬性已經(jīng)設(shè)置但不是所有屬性.. 對于這種情況,你需要 @Required 注解,請參見下面的例子:

@Required示例

Customer對象,適用@Required在 setPerson()方法,以確保 person 屬性已設(shè)置。 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注解不會強(qiáng)制執(zhí)行該屬性的檢查,還需要注冊一個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 的屬性未設(shè)置。 org.springframework.beans.factory.BeanInitializationException: Property 'person' is required for bean 'CustomerBean' 結(jié)論 嘗試@Required注解,它比依賴檢查XML文件中更加靈活,因為它可以適用于只有一個特定屬性。 定義@Required
請閱讀本文有關(guān)如何創(chuàng)建新的自定義@Required-style?注解。 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

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

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