重学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.

0 0
原创粉丝点击