目录

注解通过启用@Required注解

       @Autowired注解

@Autowired的(required=false)选项

                  @Qualifier 注解

                  Spring JSR-250 注解

注解通过<context:annotation-config/>启用

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

@Required注解

Required注解用于bean属性的set装配方法,set方法声明了Required,就必须在xml里实现装配,如果不装配就会抛出异常

Student类的内容

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类的内容

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() );

   }

}

 bean.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/>

   <!-- 如果将<property name="age"  value="11"/>注释掉,将会抛出异常 -->
   <bean id="student" class="com.xiaosousou.springHello.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>
   </bean>

</beans>

运行MainApp

 

spring 注解bean 和 xml bean 加载顺序_xml

@Autowired注解

@Autowired 注解可以在 set 方法中被用于自动装配 bean,@Autowired 可用于set方法,属性,构造函数上,默认的@Autowired 使用的都是ByType自动装配,如果找不到依赖的bean那么会抛出异常,可以通过@Autowired 的(required=false)选项,使依赖变得不是必须的

set方法上的@Autowired

TextEditor类的内容(set方法上的@Autowired)

import 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();
	}
	
}

依赖类SpellChecker的内容

public class SpellChecker {
	
	public SpellChecker() {
		System.out.println("Inside SpellChecker constructor." );
	}

	public void checkSpelling() {
		System.out.println("Inside checkSpelling." );
	}  
	
}

MainApp的内容

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
		TextEditor te = (TextEditor) context.getBean("textEditor");
		te.spellCheck();
		
	}

}

bean.xml(没有在bean中装配)

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

   <bean id="textEditor" class="com.xiaosousou.springHello.TextEditor">
   </bean>

   <bean id="spellChecker" class="com.xiaosousou.springHello.SpellChecker">
   </bean>

</beans>

运行MainApp

spring 注解bean 和 xml bean 加载顺序_spring_02

装配成功

属性上的@Autowired

通过属性的类型,实现自动装配

更改TextEditor类的内容

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
	@Autowired
	private SpellChecker spellChecker;
	
	
	public SpellChecker getSpellChecker( ) {
		return spellChecker;
	}
	
	public void spellCheck() {
		spellChecker.checkSpelling();
	}
	
}

运行MianApp

spring 注解bean 和 xml bean 加载顺序_xml_03

一样的结果

构造函数上的@Autowired

更改TextEditor类的内容

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
	
	private SpellChecker spellChecker;
	
	@Autowired
	public void TextEditor(SpellChecker spellChecker) {
		this.spellChecker=spellChecker;
	}
	
	public void spellCheck() {
		spellChecker.checkSpelling();
	}
	
}

运行MainApp

spring 注解bean 和 xml bean 加载顺序_xml_04

成功装配

@Autowired的(required=false)选项

默认情况下,@Autowired 注解意味着依赖是必须的,它类似于 @Required 注释,然而,你可以使用 @Autowired 的 (required=false) 选项关闭默认行为。

这样就算不装配age也没有任何问题

import org.springframework.beans.factory.annotation.Autowired;
public class Student {

   private Integer age;
   private String name;

   @Autowired(required=false)
   public void setAge(Integer age) {
      this.age = age;
   }  

   public Integer getAge() {
      return age;
   }

   @Autowired
   public void setName(String name) {
      this.name = name;
   }   

   public String getName() {
      return name;
   }

}

@Qualifier 注解

可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。通过Quealifier注解可以指定装配那一个bean

Student类

public class Student {

	private Integer age;
	private String name;
	
	public void setAge(Integer age) {
		this.age = age;
	}   
	
	public Integer getAge() {
		return age;
	}
	
	public void setName(String name) {
		this.name = name;
	}  
	
	public String getName() {
		return name;
	}

}

Profile类(指定装配student1 bean)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

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

MainApp类

public class MainApp {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
		Profile profile = (Profile) context.getBean("profile");
		profile.printAge();
		profile.printName();

	}

}

bean.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/>

   <bean id="profile" class="com.xiaosousou.springHello.Profile">
   </bean>

   <bean id="student1" class="com.xiaosousou.springHello.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>
   </bean>

   <bean id="student2" class="com.xiaosousou.springHello.Student">
      <property name="name"  value="Nuha" />
      <property name="age"  value="2"/>
   </bean>

</beans>

运行MainApp

.

spring 注解bean 和 xml bean 加载顺序_System_05

 可以看到自动装配的是student1的bean

Spring JSR-250 注解

Spring还使用基于 JSR-250 注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注解

@PostConstruct, @PreDestroy 注解.

@PostConstruct 注解作为初始化回调函数的一个替代

@PreDestroy 注解作为销毁回调函数的一个替代

HelloWorld类(通过注解实现初始化函数和销毁函数)

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class HelloWorld {

	private String message;
	public void setMessage(String message){
		this.message  = message;
	}
	public String getMessage(){
		System.out.println("Your Message : " + message);
		return message;
	}
	@PostConstruct
	public void init(){
		System.out.println("Bean is going through init.");
	}
	@PreDestroy
	public void destroy(){
		System.out.println("Bean will destroy now.");
	}


}

MainApp(通过AbstractApplicationContext实现销毁)

public class MainApp {

	public static void main(String[] args) {

		AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
		HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
		obj.getMessage();
		context.registerShutdownHook();

	}

}

 Bean.xml(并没有指定init函数和destroy函数)

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

   <bean id="helloWorld" class="com.xiaosousou.springHello.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

运行MainApp

spring 注解bean 和 xml bean 加载顺序_System_06

成功调用初始化函数和销毁函数

@Resource 注释

你可以在字段中或者 setter 方法中使用 @Resource 注释,它和在 Java EE 5 中的运作是一样的。@Resource 注释使用一个 ‘name’ 属性,该属性以一个 bean 名称的形式被注入。你可以说,它遵循 by-name 自动连接语义,如下面的示例所示:

import javax.annotation.Resource;
public class TextEditor {

   private SpellChecker spellChecker;

   @Resource(name= "spellChecker")
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }

   public SpellChecker getSpellChecker(){
      return spellChecker;
   }

   public void spellCheck(){
      spellChecker.checkSpelling();
   }

}

 他将自动装配id为spellChecker的bean,如果不指定name,那么如果在属性上,则使用属性名,如果在set方法上,那么使用的是参数名