欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

你所不知道的Spring自动注入详解

程序员文章站 2022-06-22 09:42:25
自动注入和@autowire@autowire不属于自动注入!注入方式(重要)在spring官网上(文档),定义了在spring中的注入方式一共有两种:set方法和构造函数。也就是说,你想在a类里面注...

自动注入和@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种)

你所不知道的Spring自动注入详解

先根据官方文档提供的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());

	}
}

控制台:

你所不知道的Spring自动注入详解

如果这时我们把xml文件中的default-autowire去掉,重新启动程序并查看控制台:

你所不知道的Spring自动注入详解

纸上得来终觉浅,绝知此事要躬行!希望大家也可以自己写写代码再验证一下!

总结

到此这篇关于你所不知道的spring自动注入详解的文章就介绍到这了,更多相关spring自动注入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!