av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

通過實例學習Spring @Required注釋原理

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

@Required 注釋應用于 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時必須放在 XML 配置文件中,否則容器就會拋出一個 BeanInitializationException 異常。下面顯示的是一個使用 @Required 注釋的示例。

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

步驟 描述1 創(chuàng)建一個名為 SpringExample 的項目,并且在所創(chuàng)建項目的 src 文件夾下創(chuàng)建一個名為 com.tutorialspoint 的包。2 使用 Add External JARs 選項添加所需的 Spring 庫文件,就如在 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)容,并且按如下解釋的那樣運行應用程序。下面是 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 配置文件,讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將引起 BeanInitializationException 異常,并且會輸出一下錯誤信息和其他日志消息:

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

<?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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美在线a| 国产精品美女一区二区 | 日韩在线观看中文字幕 | 欧美日韩一二三区 | 国产精品久久久久久久模特 | 国产一区二区在线免费观看 | 国产一区二区三区 | 日韩三级免费观看 | aa级毛片毛片免费观看久 | 九九综合九九 | 欧美日韩一区二区在线观看 | av在线一区二区三区 | 精品成人佐山爱一区二区 | 国产精品永久在线观看 | 成人欧美一区二区三区在线播放 | 日韩精品一 | 国产免费又黄又爽又刺激蜜月al | 亚洲精品在线国产 | 美女天堂在线 | 免费九九视频 | 国产高清一区二区三区 | 一二三在线视频 | 午夜电影合集 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 天天曰天天曰 | 精品日韩欧美一区二区 | 亚洲一区中文字幕在线观看 | 成人夜晚看av | 新91| 91久久精品国产91久久性色tv | 国产精品高清在线 | 日本a在线 | 欧美人妇做爰xxxⅹ性高电影 | 欧美极品视频在线观看 | 中文精品视频 | 久久精品毛片 | 日日夜夜91 | 日韩一区二区福利 | 三级在线观看 | 在线看日韩 | 国产精品日韩高清伦字幕搜索 |