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

您的位置:首頁技術文章
文章詳情頁

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

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

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

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

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

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 文件的內容:

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: 文件的內容:

<?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>一旦你已經完成的創建了源文件和 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>

現在上面的示例將產生如下結果:

Name : ZaraAge : 11

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产精品一区二区久久精品涩爱 | 亚洲高清综合 | 久久久久国产精品免费网站 | 国产自愉自愉全免费高清 | 国产色产综合色产在线观看视频 | 国产成人亚洲毛片 | 亚洲精品美女久久久 | 欧美日韩色视频 | 达达兔午夜起神影院在线观看麻烦 | 国产精品人人视频 | 国产一区在线视频 | 青草国产| 久久这里只有精品66re99 | 亚洲国产成人va在线观看 | 久久视屏这里只有精品6国产 | 精品欧美一区二区在线观看欧美熟 | 国产高清好大好夹受不了了 | 日产欧美亚洲 | 在线观看香蕉视频 | 久久婷婷色综合老司机 | 国产精品国产三级国产普通话对白 | 九一国产在线观看 | 久久美女网| 天天躁日日躁狠狠躁中文字幕老牛 | 91在线 | 国产精品v免费视频 | 日本免费大黄在线观看 | 91啦在线视频 | 国产思思| 亚洲香蕉国产高清在线播放 | 欧美视频在线免费播放 | 亚洲欧美日韩中文字幕在线不卡 | 成人私拍福利视频在线 | 久久久国产精品福利免费 | 国产高清尿小便嘘嘘视频 | 欧美一级aa免费毛片 | 黄工厂精品视频在线观看 | 亚洲视频一区在线观看 | 国产欧美日韩精品第一区 | 日韩一区国产二区欧美三区 | 国产精品久久久久久久免费大片 |