你所不知道的Spring自动注入详解
作者:ZeS丶 时间:2021-09-04 19:30:08
自动注入和@Autowire
@Autowire不属于自动注入!
注入方式(重要)
在Spring官网上(文档),定义了在Spring中的注入方式一共有两种:set方法和构造函数。
也就是说,你想在A类里面注入另外一个B类,无论你是通过写 XML文件,或者通过 @Autowried,他们最终都是通过这个A类的set方法或者构造函数,将B类注入到A类中!
换句话说,你如果A类里面没有setB(B b){…},那你就别想通过set方法把B类注入到A类中
自动注入
首先摆出一个比较颠覆的观点:@Autowire不属于自动注入!
如果要讨论自动注入,我们先要了解什么是自动注入,什么是手动注入。
手动注入:在Spring 1.x的时候,我们想要在A类中注入B类,我们只能通过在xml配置文件中,加上< property >标签。也就是说,如果我们想在A类中注入100个类,我们就要重复着写100个< property > 。而Spring为了我们能少码点字,就提供了 @Autowired 注解,通过这个注解,我们就可以更加轻松的手动注入需要的类
自动注入:如果在A类里面,需要用到B类,C类等等…我不需要重复着写100个< property >或者100个@Autowired。而是只需要注明需要哪些类即可
既然是自动,那就代表我啥都不用做,就连一个 @Autowire 我都不加的情况下我让B类注入进A类,这才算真正的自动注入
证明:
首先,我们先看看最原始的,通过xml的注入类:
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested ref element -->
<property name="beanOne">
<ref bean="anotherExampleBean"/>
</property>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
对应的类:
public class ExampleBean {
private AnotherBean beanOne;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
}
这是Spring官网上的一个例子,在最开始,我们通过在XML中追加<property>属性来为类加上其所需要的类。这种手动注入的方式十分的繁琐,所以后面出现了@Autowire注解来进行注入!说白了,就是人们为了偷懒,用一个@Autowire注解代替了写一大串的property属性!(先这么理解,底层源码肯定不是这么做的!)这样的话,还能说 @Autowire 是自动注入了吗?
对于自动注入,在Spring中提供了下面4种方式(甚至我可以更加负责任的告诉你们,在Spring源码中有5种)
先根据官方文档提供的4种方法进行解释:
no:就是不要自动装配
byName:通过名字进行自动装配
byType:通过类型进行自动装配
constructor:通过构造函数进行自动装配
最开始我有写到,在Spring中,自动注入的方式就只有两种,通过set()方法和构造函数。所以 byName和 byType 都是通过 set()进行装配的。
代码演示:通过byType方式进行自动注入
通过在<beans>
标签的末尾加上 default-autowire="byType"
来实现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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
default-autowire="byType">
<bean id="defaultAutowireService" class="com.spring.autowiringModes.DefaultAutowireService">
</bean>
<bean id="byTypeDemo" class="com.spring.autowiringModes.ByTypeDemo"/>
</beans>
Java类:
public class DefaultAutowireService {
ByTypeDemo byTypeDemo;
public ByTypeDemo getByTypeDemo() {
return byTypeDemo;
}
public void setByTypeDemo(ByTypeDemo byTypeDemo) {
this.byTypeDemo = byTypeDemo;
}
}
启动类:
public class XmlTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
DefaultAutowireService bean =
context.getBean("defaultAutowireService", DefaultAutowireService.class);
System.out.println(bean.getByTypeDemo());
}
}
控制台:
如果这时我们把xml文件中的default-autowire
去掉,重新启动程序并查看控制台:
纸上得来终觉浅,绝知此事要躬行!希望大家也可以自己写写代码再验证一下!
来源:https://blog.csdn.net/weixin_46357454/article/details/109125108
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java基础教程之static五大应用场景
Android 实现永久保存数据的方法详解
SpringSecurity报错authenticationManager must be spec的解决
![](https://img.aspxhome.com/file/2023/4/60814_0s.png)
java必学必会之线程(2)
![](https://img.aspxhome.com/file/2023/2/58742_0s.png)
Java使用Tess4J实现图像识别方式
![](https://img.aspxhome.com/file/2023/9/60659_0s.png)
java遍历properties文件操作指南
Java8之lambda最佳实践_动力节点Java学院整理
Maven+SSM框架实现简单的增删改查
![](https://img.aspxhome.com/file/2023/0/59720_0s.png)
浅析Java内存模型与垃圾回收
![](https://img.aspxhome.com/file/2023/5/59965_0s.png)
Java Arrays.asList使用方法解析
详解Spring全局异常处理的三种方式
java中使用数组进行模拟加密的方法
Mybatis日志模块的适配器模式详解
IDEA SpringBoot项目配置热更新的步骤详解(无需每次手动重启服务器)
![](https://img.aspxhome.com/file/2023/9/59659_0s.jpg)
Java算法比赛常用方法实例总结
![](https://img.aspxhome.com/file/2023/4/59984_0s.png)
SpringBoot打成war包在tomcat或wildfly下运行的方法
Java使用BigDecimal进行运算封装的实际案例
Android编程基础之获取手机屏幕大小(DisplayMetrics应用)示例
![](https://img.aspxhome.com/file/2023/3/85983_0s.jpg)
Java Swing JTextArea文本区域的实现示例
![](https://img.aspxhome.com/file/2023/6/58606_0s.png)
Springboot项目快速实现过滤器功能
![](https://img.aspxhome.com/file/2023/9/57729_0s.png)