重学Spring(三)基于注解的配置
来源:互联网 发布:淘宝小图标素材 编辑:程序博客网 时间:2024/05/15 12:08
想要启用Spring的注解,首先最重要的一点,要在Spring的配置文件中声明启用注解。
<?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/></beans>
常用注解介绍:
1.@Required
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 @Required 注释的示例。
public class Student { private String name; private Integer age; public String getName() { return name; } /** * required注解表明该属性在配置时必须放在 XML 配置文件中 * @param name */ @Required public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
xml配置文件中表明name属性必须配置
<bean id="student" class="com.tutorialspoint.entity.Student"> <property name="name" value="hexiang"></property> <!--<property name="age" value="24"></property> age属性的setter方法没有标记required所以可以不在此配置,并不会报错 --> </bean>
2.@Autowaire
@Autowired 的(required=false)选项
默认情况下,@Autowired 注释意味着依赖是必须的,它类似于 @Required 注释,然而,你可以使用 @Autowired 的 (required=false) 选项关闭默认行为。
@Autowired 注释可以在 setter 方法中被用于自动装配 bean。当 Spring遇到一个在 setter 方法中使用的@Autowired 注释,它会在方法中视图执行 byType 装配。
public class TextEditor { private SpellChecker spellChecker; @Autowired public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker( ) { return spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); }}
xml
<bean id="textEditor" class="com.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean>
@Autowired 注解可以声明在属性上,这样可以去除setter方法。如下
public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println("Inside TextEditor constructor." ); } /** 没有setter方法 */ public SpellChecker getSpellChecker( ){ return spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); }
构造函数中的 @Autowired
@Autowired 使用在构造函数上说明当创建 bean 时,即使在 XML 文件中没有使用元素配置 bean ,构造函数也会被自动装配。让我们检查一下下面的示例。
这里是 TextEditor.java 文件的内容:
package com.tutorialspoint;import org.springframework.beans.factory.annotation.Autowired;public class TextEditor { private SpellChecker spellChecker; @Autowired public TextEditor(SpellChecker spellChecker){ System.out.println("Inside TextEditor constructor." ); this.spellChecker = spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); }}
下面是配置文件 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 textEditor bean without constructor-arg --> <bean id="textEditor" class="com.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>
3.@Qualifier
可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。
例如:以下有两个相同类型的bean
<bean id="student1" class="com.tutorialspoint.Student"> <property name="name" value="Zara" /> <property name="age" value="11"/> </bean> <!-- Definition for student2 bean --> <bean id="student2" class="com.tutorialspoint.Student"> <property name="name" value="Nuha" /> <property name="age" value="2"/> </bean>
为了在自动装配时候使程序知道装配哪个bean,可以采用如下方式:
public class Profile { @Autowired @Qualifier("student1") private Student student; public Profile(){ System.out.println("Inside Profile constructor." ); } public void printAge() { System.out.println("Age : " + student.getAge() ); } public void printName() { System.out.println("Name : " + student.getName() ); }}
4.
- 重学Spring(三)基于注解的配置
- spring(三)(基于注解的方式配置bean)
- Spring 基于注解的配置(三)(@Resource,@PostConstruct ,@PreDestory)
- spring基于注解的配置
- Spring基于注解的配置
- spring基于注解的配置
- Spring In Action(三):基于注解的AOP
- Spring核心三(基于注解的组件扫描)
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- Struts2基于注解Annotation的零配置开发(三)
- Spring依赖注入的简单示例(基于注解配置)
- spring-基于注解(Annotation)的配置(转)
- spring + maven 基于注解的配置(2)
- (十三)Spring框架——基于注解的配置
- SSM搭建-Spring mvc基于注解的配置(13)
- spring 基于注解的控制器配置
- Spring之基于注解的容器配置
- [Spring]基于注解的形式配置Bean
- 普通自适应遗传算法(AGA)参数的计算公式 解读篇
- Windows下Redis安装与使用
- codeforces777C
- Maven初次使用之问题整理
- 高数引论学习笔记1
- 重学Spring(三)基于注解的配置
- python 网络爬虫入门-Urllib库的基本使用
- SQL脚本
- 大牛技术博客推荐
- English Interview Topic
- jQuery基本语法
- (SRCNN)Image super-resolution using deep convolutional networks
- WPF 遍历所选文件夹下的所有文件和子文件下的文件
- 编写高质量OC代码52建议总结:26.不要在分类中设置属性