1:说明

spring为了方便我们完成bean注入相关的配置工作,提供了自动注入功能,我们只需要按照其规则来进行配置,就能够让spring注入我们需要的bean了。

2:准备测试类

2.1:State

public class State {private String stateName;...snip...
}

2.2:Country

public class Country {// generate setters...private State state; // secondary typepublic void setState(State state) {this.state = state;}// print injected value on the console logpublic void display() {System.out.println("State name is: " + state.getStateName());}
}

3:autowire->no

这也是默认的自动注入的方式,即不使用自动注入,这种方式需要我们显示的在配置文件中通过<property>标签然后通过ref属性来设置,这也是默认的自动注入方式。

3.1:配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="no"><property name="state" ref="state"/></bean>
</beans>

3.2:测试

@Test
public void test_autowire_no() {ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("autowiretest/test-autowire-no.xml");System.out.println(ac.getBean("country", Country.class).getState());
}

运行:

yudaosourcecode.autowiretest.State@42607a4fProcess finished with exit code 0

4:autowire->byType

这种方式是通过属性的类型,来进行自动注入。

4.1:配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="byType"></bean>
</beans>

可以看到此时就不需要通过<property>标签来配置了。

4.2:测试

@Test
public void test_autowire_bytype() {ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("autowiretest/test_autowire_bytype.xml");System.out.println("autowire byType: ");System.out.println(ac.getBean("country", Country.class).getState());
}

运行:

autowire byType:
yudaosourcecode.autowiretest.State@646007f4Process finished with exit code 0

但是当相同类型的bean有多个的时候byType的方式就不适用了,因为无法确定到底注入哪个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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State"/><bean id="state1" class="yudaosourcecode.autowiretest.State"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="byType"/>
</beans>

再次运行程序:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'country' defined in class path resource
[autowiretest/test_autowire_bytype.xml]:
Unsatisfied dependency expressed through bean property 'state'; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying
bean of type 'yudaosourcecode.autowiretest.State' available:
expected single matching bean but found 2: state,state1

出现这个异常的原因就是,相同类型的bean有两个,无法确定是哪个,即候选者bean有多个,为了解决这个问题,spring提供了autowire-candidate属性,代表在自动注入时是否作为候选bean,默认值是true,这里我们可以将其中一个设置为false,修改配置文件为如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State"/><bean id="state1" class="yudaosourcecode.autowiretest.State" autowire-candidate="false"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="byType"/>
</beans>

再次运行程序,就可以正常完成注入了:

autowire byType:
yudaosourcecode.autowiretest.State@646007f4Process finished with exit code 0

不使用autowire-candidate的话也可以使用primary来指定首选bean,即设置一个优先级最高的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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State" primary="true"/><bean id="state1" class="yudaosourcecode.autowiretest.State"/><bean id="anotherState"class="yudaosourcecode.autowiretest.AnotherState"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="byType"lazy-init="true"/>
</beans>

5:autowire->byName

这种是通过属性的名称来作为目标bean的名称来完成自动注入,因为是通过名称,而spring的bean名称是不允许重复的,因此这种情况下相同类型的bean有多个,是不会有任何问题的。

5.1:配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State"/><bean id="state1" class="yudaosourcecode.autowiretest.State"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="byName"/>
</beans>

5.2:测试

@Test
public void test_autowire_byname() {ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("autowiretest/test_autowire_byname.xml");System.out.println("autowiretest/autowire byname: ");System.out.println(ac.getBean("country", Country.class).getState());
}

运行:

autowiretest/autowire byname:
yudaosourcecode.autowiretest.State@43a0cee9Process finished with exit code 0

6:autowire->constructor

byTypebyName都是自动完成属性的注入,这里的constructor是自动完成构造函数的自动注入。

6.1:改造Country

定义一个构造函数使用state作为参数。

public class Country {private State state; // secondary typepublic Country(State state) {this.state = state;}// print injected value on the console logpublic void display() {System.out.println("State name is: " + state.getStateName());}
}

6.2:配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="state" class="yudaosourcecode.autowiretest.State"/><bean id="country"class="yudaosourcecode.autowiretest.Country"autowire="constructor"/>
</beans>

6.3:测试

@Test
public void test_autowire_constructor() {ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("autowiretest/test_autowire_constructor.xml");System.out.println("by constructor: ");System.out.println(ac.getBean("country"));
}

6.4:需要注意的问题

构造函数的自动注入,在寻找目标bean的时候,默认使用的是byType,因此当目标bean有多个时候,同样会出现异常,此时也是可以通过autowire-candidate属性来解决,这里不再演示了,和4:autowire->byType中的情况是一致的。

7:总结

各种类型说明如下表:

autowire类型 注入方式 注入目标
no 不自持自动注入,需要通过标签
<property>或者<constructor>配置
依配置决定
byType 根据类型注入 bean属性
byName 根据名称注入 bean属性
constructor 根据类型注入 构造函数参数

spring autowire在xml中的使用相关推荐

  1. Spring配置文件applicationContext.xml中bean>>property>>name属性的含义

    Spring配置文件applicationContext.xml中bean>>property>>name属性表示的含义 首先我们知道property是bean元素的子元素,它 ...

  2. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    Spring中,applicationContext.xml 配置文件在web.xml中的配置详解 2016年10月04日 15:22:26 阅读数:7936 转自http://www.cnblogs ...

  3. Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

    运行环境:Spring框架整合MaBitis框架 问题叙述: 在Spring配置文件applicationContext-mybatis.xml中配置好mybatis之后 <?xml versi ...

  4. springboot创建parent_创建springboot项目时,pom.xml中parent报错

    下载Maven 进入maven官网 http://maven.apache.org/download.cgi. 找到并点击apache-maven-3.6.2-bin.zipi.下载压缩包 配置Mav ...

  5. java this context,java – Spring XML中applicationcontext的“this”引用

    有没有办法在Spring中的bean配置文件中引用当前的应用程序上下文? 我想做这样的事情: xmlns="http://www.springframework.org/schema/bea ...

  6. springboot项目中的注解 启动项目的方式 解决spring的bean.xml配置不生效 spring的基础JDBC配置

    依赖 创建一个 Spring Boot 工程时,可以继承自一个 spring-boot-starter-parent ,也可以不继承 先来看 parent 的基本功能有哪些? 定义了 Java 编译版 ...

  7. web.xml中的contextConfigLocation在spring中的作用

    在web.xml中通过contextConfigLocation配置spring,contextConfigLocation 参数定义了要装入的 Spring 配置文件. 如果想装入多个配置文件,可以 ...

  8. web.xml中,spring模块化加载xml方式

    1:web.xml中添加监听器. <listener><listener-class>org.springframework.web.context.ContextLoader ...

  9. 5、Spring SPEL使用之--在XML中使用SPEL

    SPEL(Spring Expression Language)即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式 ...

最新文章

  1. spring cloud的网关服务Zuul
  2. matlab 小波变换_matlab小波工具箱实例(二):时频分析和连续小波变换
  3. 生产环境JVM内存大小配置
  4. 十字路口红绿灯plc程序_实例讲解红绿灯PLC程序设计方法
  5. 怎么在QQ浏览器上使用微信聊天?
  6. 【OpenCV 例程200篇】85. 频率域高通滤波器的应用
  7. 《Asp.Net 2.0 揭秘》读书笔记(九)
  8. 根据表格长度使td里的内容换行
  9. 《应用软件安全编程指南》国标发布 奇安信代码卫士已全面支持
  10. OpenCV中IplImage与Qt中的QImage转化
  11. C++ Primer Plus 读书笔记(第4、5章)
  12. 声道测试音频_功率放大器测试方法
  13. Java| Javadoc生成Java帮助文档
  14. STM32 cubemx驱动ws2812b灯条
  15. Android 如何直播RTMP流
  16. svn版本回退(CornerStone)
  17. 华为手机如何更新鸿蒙系统_华为鸿蒙系统怎么升级?升级鸿蒙系统步骤
  18. 【关于听任大佬的讲话的深刻感悟】
  19. 贪心算法求解:王者荣耀购买点券最优策略
  20. 真实机下 ubuntu 18.04 安装anaconda+cuDNN+pytorch以及其版本选择(亲测非常实用)

热门文章

  1. 内存管理笔记十、buddy伙伴系统
  2. php函数从数组中取出指定的数目,php截取数组的指定长度
  3. Spring注解驱动:组件注册(一)
  4. C语言向MySQL插入数据
  5. 反对第三方中国联通的淘宝充值
  6. SpringBoot+Vue+虹软(ArcSoft) 的一个在线人脸识别Web系统,可通过调用笔记本摄像头或者网络摄像头实时的进行人脸识别。
  7. 陕西建行成功示范中标普华Linux应用(转)
  8. 关闭Windows自带的IIS服务
  9. Google Space
  10. IDEA之Session的活化和钝化