Spring自动装配----注解装配----Spring自带的@Autowired注解

Spring自动装配----注解装配----Spring自带的@Autowired注解

父类

package cn.ychx;

public interface Person {
	
	public void sayHello();

}
学生子类
package cn.ychx;

public class Student implements Person {

	@Override
	public void sayHello() {
		System.out.println("Hello! My name is Tom, I'm a Student.");
	}

}
教师子类
package cn.ychx;

public class Teacher implements Person {

	@Override
	public void sayHello() {
		System.out.println("Hello! My name is Alice, I'm a Teacher.");
	}

}

表演类

package cn.ychx;

public interface Performer {

	public void perform();
}

玩游戏
package cn.ychx;

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

public class PlayGame implements Performer {

	@Autowired(required=false)
	@Qualifier("student")
	private Person person;
	
	@Override
	public void perform() {
		if (person == null) {
			System.out.println("Person is not exist.");
			return;
		}
		person.sayHello();
	}
}
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 启用注解支持 -->
    <context:annotation-config/>
    
    <bean id="student" class="cn.ychx.Student"/>
    <bean id="teacher" class="cn.ychx.Teacher"/>
    
    <bean id="playGame" class="cn.ychx.PlayGame">
    </bean>
</beans>

执行

package cn.ychx;

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

public class Test {
	private static ApplicationContext ctx;

	public static void main(String[] args) {
		
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Performer performer = (Performer) ctx.getBean("playGame");
		if (performer != null) {
			performer.perform();
		} else {
			System.out.println("Object is null.");
		}
	}
}

执行结果

Hello! My name is Tom, I'm a Student.

通过修改@Qualifier("teacher")可以得到结果

Hello! My name is Alice, I'm a Teacher.


示例使用的是Spring自带的@Autowired注解,实现将Person的实现类(student或者teacher)注入PlayGame类的person属性中。

1.启用Spring注解支持

<context:annotation-config/>

2.@Autowired(required=false)注解

可以标注在属性,set方法,构造器上,使用byType寻找上下文的bean。可以看到示例中标注在属性上面,这时可以省略set方法,不受private的影响。

required=false说明Bean的注入不是必须的,也就是说可以删除配置文件中的student和teacher的Bean,这时PlayGame中的person属性为null

4.@Qualifier("student")注解

用来明确指定要使用的bean(双引号中的)是谁,为什么使用这个注解,因为可以满足PlayGame中的person属性的Bean不是唯一的,有两个分别是student和teacher,

这时Sping不知道该使用那个去注入,就会抛出异常。当使用@Qualifier("student")注解,就可以明确的告诉Spring在有多个Bean满足条件是用哪个。

5.缺点是会引入对Spring的特定依赖

原文地址:https://www.cnblogs.com/yangchongxing/p/7642405.html