多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國(guó)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2

wkspring教程

Spring @Required 注釋

閱讀 (2041)

Spring @Required 注釋

@Required 注釋應(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 類 StudentMainApp
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;
   @Required
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   @Required
   public 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/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 id="student" 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 id="student" class="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>
   </bean>

</beans>

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

Name : Zara
Age : 11
關(guān)閉
程序員人生
主站蜘蛛池模板: 岛国在线123456 | 亚洲午夜国产精品无卡 | 日本一区二区三区欧美在线观看 | 欧美国产另类 | 最近最新中文字幕免费1 | 黄色小说校园春色 | 校园春色亚洲欧美 | 亚洲日本中文字幕在线 | h视频在线免费观看 | 尤物视频网站在线观看 | 久久久久久一级毛片免费野外 | 一区小说二区另类小说三区图 | 天堂精品 | 亚洲人成网址在线观看 | 天堂欧美 | 视频一区二区不卡 | 久久一本精品久久精品66 | 欧美18videosex性欧美69 | 欧美视频xxxx| 狠久久 | 一级女人18片毛片免费视频 | 欧美成性色 | 亚洲成a人片毛片在线 | 亚州三级视频 | 国产成人精品午夜在线播放 | 国产日韩一区在线精品欧美玲 | 国产在线观看一区二区三区 | 伊人五月天综合 | 最近最新视频中文字幕3 | 久久久久久国产精品视频 | 国产福利在线观看精品 | 国产日韩精品一区二区在线观看播放 | 超清中文乱码精品字幕在线观看 | 免费在线视频一区 | 亚洲成人高清 | 日韩中文字幕一区二区不卡 | 国产丝袜福利视频在线播放 | 亚洲精品中文字幕无乱码 | 日本特黄特色大片免费播放视频 | 动漫羞羞网站 | videos雌雄同体xxxx视频 |