Spring入门总结(四)基于注解的配置

@Required 注释应用于 bean 属性的 setter 方法

用法:java代码必须有成员变量的setter方法,该方法前要有@Required关键字,xml中有配置。

如果有注解的方法,但是xml中没有配置,则会报错的。所以可以将该注解理解需要实现该方法。

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

xml

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

此时xml中的属性的 必须要和@Required相互匹配,否则会报错。

@Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。

用法:你可以给构造方法或者成员属性实现自动注入。

如果属性中的成员是一个类的对象,就只需要xml配置中有他的bean,并且构造函数有关键字@Autowired即可。

mport org.springframework.beans.factory.annotation.Autowired;
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

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

此时他会自动识别bean,但是如果你的构造函数含有其他的基本数据类型的话,就必须设置自动装配。

注意:Autowired是在BeanPostProcessor里面处理的,所以不能在BeanPostProcessor,也不能在BeanFactoryBeanPostProcessor应用这些注解。但可以通过@Bean 或者xml配置加载。

public class Student {
	  private SpellChecker spellChecker;
	  private String name;
	  private Integer age;
	  
	public SpellChecker getSpellChecker() {
		return spellChecker;
	}
	public void setSpellChecker(SpellChecker spellChecker) {
		this.spellChecker = spellChecker;
	}
	@Autowired
	  public Student(SpellChecker spellChecker, String name) {
		super();
		this.spellChecker = spellChecker;
		this.name = name;
	}
//	@Autowired(required=false)
	   public void setAge(Integer age) {
	      this.age = age;
	   }  
	   public Integer getAge() {
	      return age;
	   }
//	   @Autowired
//	   @Required
	   public void setName(String name) {
	      this.name = name;
	   }   
	   public String getName() {
	      return name;
	   }
}

xml

<bean id="student2" class="com.tutorialspoint.Student" autowire="constructor">
      <constructor-arg value="as"></constructor-arg>
   </bean>
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"></bean>

聪明的你可能会想到,那万一有更个同种类型的bean怎么办呢?为了消除这种混乱,就有个@Qualifier 注释。

用法:
 在xml中配置有相同类型的bean,

<!-- Definition for student1 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>

在成员变量前加

@Autowired @Qualifier("student1")

private Student student; 

可以理解为,使student1具备自动装配的资格。

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/81386215