通过实例学习Spring @Required注释原理

 更新时间:2020年03月23日 14:31:42   作者:w3cschool  
这篇文章主要介绍了通过实例学习Spring @Required注释原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

@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;
@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 文件的内容:

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/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>
一旦你已经完成的创建了源文件和 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 id="student" class="com.tutorialspoint.Student">
   <property name="name" value="Zara" />
   <property name="age" value="11"/>
  </bean>

</beans>

现在上面的示例将产生如下结果:

Name : Zara
Age : 11

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Springboot2 配置AOP日志的方法步骤

    Springboot2 配置AOP日志的方法步骤

    这篇文章主要介绍了Springboot2 配置AOP日志的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • SpringBoot + Spring Cloud Consul 服务注册和发现详细解析

    SpringBoot + Spring Cloud Consul 服务注册和发现详细解析

    这篇文章主要介绍了SpringBoot + Spring Cloud Consul 服务注册和发现,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • Java如何重写object类的equals方法详解

    Java如何重写object类的equals方法详解

    这篇文章主要给大家介绍了关于Java如何重写object类的equals方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • IDEA使用SpringAssistant插件创建SpringCloud项目

    IDEA使用SpringAssistant插件创建SpringCloud项目

    IDEA 功能强大,可以用来高效的开发应该程序。它还支持第三方插件、用户可以根据需要添加自己喜欢的插件。下面介绍如何使用 IDEA 创建 Spring Cloud 项目
    2021-06-06
  • Java数据结构之链表详解

    Java数据结构之链表详解

    本篇文章我们将讲解一种新型的数据结构—链表,链表是一种使用广泛的通用数据结构,它可以用来作为实现栈,队列等数据结构的基础.文中有非常详细的介绍,需要的朋友可以参考下
    2021-05-05
  • Java利用文件输入输出流实现文件夹内所有文件拷贝到另一个文件夹

    Java利用文件输入输出流实现文件夹内所有文件拷贝到另一个文件夹

    这篇文章主要介绍了Java实现文件夹内所有文件拷贝到另一个文件夹,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • 常用输入字节流InputStream介绍

    常用输入字节流InputStream介绍

    下面小编就为大家带来一篇常用输入字节流InputStream介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • SpringBoot启动流程之引导上下文DefaultBootstrapContext的过程

    SpringBoot启动流程之引导上下文DefaultBootstrapContext的过程

    本文详细介绍了SpringBoot版本2.7.18中SpringApplication的run方法,引导注册组件初始化器BootstrapRegistryInitializer是SpringBoot的第一个扩展点,负责应用启动早期阶段的初始化和配置,感兴趣的朋友跟随小编一起看看吧
    2024-11-11
  • java报错之springboot3+vue2项目web服务层报错总结

    java报错之springboot3+vue2项目web服务层报错总结

    java入门学习,随手记录一下开发过程中产生的报错,有些错误是网上搜索再加上自己尝试,随手引用了一些其他人的记录,也是留给自己看的,或是希望能对其他初学者有帮助

    2023-06-06
  • java实现租车系统

    java实现租车系统

    这篇文章主要为大家详细介绍了java实现租车系统,以及遇到的两个问题解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01

最新评论