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的特定依赖
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端