亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

通過(guò)實(shí)例學(xué)習(xí)Spring @Required注釋原理

瀏覽:2日期:2023-09-14 11:26:20

@Required 注釋?xiě)?yīng)用于 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時(shí)必須放在 XML 配置文件中,否則容器就會(huì)拋出一個(gè) BeanInitializationException 異常。下面顯示的是一個(gè)使用 @Required 注釋的示例。

示例:讓我們使 Eclipse IDE 處于工作狀態(tài),請(qǐng)按照下列步驟創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟 描述1 創(chuàng)建一個(gè)名為 SpringExample 的項(xiàng)目,并且在所創(chuàng)建項(xiàng)目的 src 文件夾下創(chuàng)建一個(gè)名為 com.tutorialspoint 的包。2 使用 Add External JARs 選項(xiàng)添加所需的 Spring 庫(kù)文件,就如在 Spring Hello World Example 章節(jié)中解釋的那樣。3 在 com.tutorialspoint 包下創(chuàng)建 Java 類 Student 和 MainApp。4 在 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml。5 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并且按如下解釋的那樣運(yùn)行應(yīng)用程序。下面是 Student.java 文件的內(nèi)容:

package com.tutorialspoint;import org.springframework.beans.factory.annotation.Required;public class Student {private Integer age;private String name;@Requiredpublic void setAge(Integer age) {this.age = age;}public Integer getAge() {return age;}@Requiredpublic void setName(String name) {this.name = name;}public String getName() {return name;}}下面是 MainApp.java 文件的內(nèi)容:

package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext('Beans.xml');Student student = (Student) context.getBean('student');System.out.println('Name : ' + student.getName() );System.out.println('Age : ' + student.getAge() );}}下面是配置文件 Beans.xml: 文件的內(nèi)容:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd'>

<context:annotation-config/>

<!-- Definition for student bean --><bean class='com.tutorialspoint.Student'><property name='name' value='Zara' />

<!-- try without passing age and check the result --><!-- property name='age' value='11'--></bean>

</beans>一旦你已經(jīng)完成的創(chuàng)建了源文件和 bean 配置文件,讓我們運(yùn)行一下應(yīng)用程序。如果你的應(yīng)用程序一切都正常的話,這將引起 BeanInitializationException 異常,并且會(huì)輸出一下錯(cuò)誤信息和其他日志消息:

Property ’age’ is required for bean ’student’下一步,在你按照如下所示從 “age” 屬性中刪除了注釋,你可以嘗試運(yùn)行上面的示例:

<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd'> <context:annotation-config/> <!-- Definition for student bean --> <bean class='com.tutorialspoint.Student'> <property name='name' value='Zara' /> <property name='age' value='11'/> </bean></beans>

現(xiàn)在上面的示例將產(chǎn)生如下結(jié)果:

Name : ZaraAge : 11

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 我要看一级黄色片 | 2021久久精品国产99国产精品 | 黄在线观看免费 | 精品一区亚洲 | 成人免费视频一区二区 | 亚洲午夜精品国产电影在线观看 | 美女黄色一级毛片 | 欧美专区在线观看 | 精品亚洲一区二区在线播放 | 亚洲色图视频在线观看 | 草草影院一级毛片a级 | 黄色的视频免费 | 午夜久草 | 欧美线人一区二区三区 | 久久国产精品二区99 | 国产精品福利久久香蕉中文 | 亚洲第一在线视频 | 国内在线网友露脸自拍 | 午夜黄色一级片 | 操出水视频 | 成 人 黄 色 视频播放1 | 日韩精品一区二三区中文 | 那一个欧美一级毛片 | 欧美一级视频在线观看 | 欧美亚洲国产另类在线观看 | 久久这里只有精品首页 | 欧美日韩在线播放一区二区三区 | 免费一区在线观看 | 看国产黄色片 | 亚洲国产片高清在线观看 | 一区二区免费视频观看 | 午夜影院在线视频 | 成人在线免费网站 | 色综合图片二区150p | 日韩在线不卡 | 一级毛片在线看在线播放 | 精品国产中文字幕 | 免费看又爽又黄禁片视频1000 | 色图综合 | 国产精选在线播放 | 99久久亚洲国产高清观看 |