@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。

java

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;
}
}

会抛出异常的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/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>

不会抛出异常的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/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>

@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。的更多相关文章

  1. Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

    XML配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  2. 重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件

    重写UITableViewCell子类中属性的setter方法来实现隐藏或显示该cell中的某些控件 为什么会需要这样子的一种方法来实现隐藏或者显示一个cell中的某些控件呢? 其实,隐藏cell中某 ...

  3. JAVA 抛出与声明异常

    在编程过程中,我们往往会遇到这种情况,在当前环境中无法解决,比如用户传入的参数错误,IO设备问题等.此时,就要从当前环境中抛出异常提交给上级来处理. 在JAVA语言中,使用throw关键字来抛出异常. ...

  4. shiro配置unauthorizedUrl,无权限抛出无权限异常,但是不跳转

    在使用shiro配置无授权信息的url的时候,发现这样的一个scenario,配置好unauthorizedUrl后仍然无法跳转,然后就在网上开始找,找了原因以及解决方案 原因,先post一个源码: ...

  5. 【JAVASE】Java同一时候抛出多个异常

    Java有异常抛出后.跳出程序.一般无法运行接下来的代码. 大家做登陆功能.常常会实username和password的登陆校验,username或者password错误.假设通常是提示usernam ...

  6. C++学习40 抛出自己的异常

    throw 是C++中的关键字,用来抛出异常.如果不使用 throw 关键字,try 就什么也捕获不到:上节提到的 at() 函数在内部也使用了 throw 关键字来抛出异常. throw 既可以用在 ...

  7. Python面向对象之异常捕获(一)-----抛出一个异常

    大部分的异常都继承自Exception这个类(而这个类有继承自BaseException这个类) 常见的异常 ValueError TypeError IndexError 抛出一个异常 下面这个类的 ...

  8. [Go] 如何正确地 抛出 错误 和 异常(error/panic/recover)?

    序言 错误 和 异常 是两个不同的概念,非常容易混淆.很多程序员习惯将一切非正常情况都看做错误,而不区分错误和异常,即使程序中可能有异常抛出,也将异常及时捕获并转换成错误.从表面上看,一切皆错误的思路 ...

  9. WebService如何抛出干净的异常

    转载:http://www.cnblogs.com/ahdung/p/3953431.html 说明:[干净]指的是客户端在捕获WebService(下称WS)抛出的异常时,得到的ex.Message ...

随机推荐

  1. 6.00.1x Introduction to computation

    6.00 Introduction to Computer Science                  and  Programming • Goal: –Become skillful at ...

  2. MyBatis-Generator的配置说明和使用

    关于MyBatis: MyBatis Generator (MBG) 是一个Mybatis的代码生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各个版本的代码,和iBATIS 2. ...

  3. 玩转微信小程序

    原文链接 2007 年 1 月 9 号,苹果一代在功能机盛行的年代中出世. 2017 年 1 月 9 号,微信小程序在重型app风靡的压力下上线. 苹果的出世掀起了互联网一波又一波的浪潮,而微信小程序 ...

  4. WinForm 进程、线程

    一.进程 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动. 它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体. Process 类,用来操作进程. 命名空间:using Sys ...

  5. shell 快速入门

    1: 脚本开始行 #!/bin/bash 这一行表明,不管用户选择的是那种交互式shell,该脚本需要使用bash shell来运行. 由于每种shell的语法大不相同,所以这句非常重要. 2:变量 ...

  6. linux驱动的多种init函数及其调用顺序

    在驱动设计时可以选用多种驱动初始化函数达到控制驱动初始化顺序控制,其中level(__define_initcall的第一个参数即优先级)越小优先级越高, #define pure_initcall( ...

  7. Picasso 修改缓存路径

    Picasso 是 Square 公司开源的一个非常友好的图片加载框架,使用范围也比较广泛.具体的使用这里就不做介绍了,文章主要讲讲如何修改图片的缓存路径.Picasso默认的缓存路径位于data/d ...

  8. 百度云推送----iOS

    前言 记录一下这几天学习的百度推送,觉得这个东西弄的还挺糟心的,好多注意的地方 正文 1.先申请一个百度开发者账号 http://push.baidu.com/fc 2.创建一个新应用,并应用配置 3 ...

  9. udp 服务器界面监听

    今天在做项目的时候,发现不同子网下是不能做UDP通信的,不知道是不是这样呢 遇到一个错误:eclipse遇到报错: The type JPEGImageEncoder is not accessibl ...

  10. css中的text-overflow

    css中的text-overflow HTML中: <body><div class="clip">此处中多余的文字直接被切掉,不显示</div> ...