【Spring实战】—— 8 自动装配
本篇介绍一下自动装配的知识,Spring为了简化配置文件的编写。采用自动装配方式,自动的装载需要的bean。
自动装配 有以下几种方式:
1 byName 通过id的名字与属性的名字进行判断,要保证Bean实例中属性名字与该装配的id名字相同。
2 byType 通过类型确定装配的bean,但是当存在多个类型符合的bean时,会报错。
3 contructor 在构造注入时,使用该装配方式,效果如同byType。
4 autodetect 自动装配,这个测试了,3.0.5版本不可用了,不知道是不是被移除了。
下面简单的看下,自动装配的所需代码:
- public class Instrumentalist implements Performer{
- private String song;
- private int age;
- private Instrument instrument;
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getSong() {
- return song;
- }
- public void setSong(String song) {
- this.song = song;
- }
- public Instrument getInstrument() {
- return instrument;
- }
- public void setInstrument(Instrument instrument) {
- this.instrument = instrument;
- }
- public Instrumentalist(){}
- public Instrumentalist(String song,int age,Instrument instrument){
- this.song = song;
- this.age = age;
- this.instrument = instrument;
- }
- public void perform() throws PerformanceException {
- System.out.println("Instrumentalist age:"+age);
- System.out.print("Playing "+song+":");
- instrument.play();
- }
- }
- public interface Instrument {
- public void play();
- }
- public class Saxophone implements Instrument {
- public Saxophone(){}
- public void play() {
- System.out.println("TOOT TOOT TOOT");
- }
- }
- public class test {
- public static void main(String[] args) throws PerformanceException {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
- Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
- performer.perform();
- }
- }
采用byName方式的配置文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="instrument" class="com.spring.test.setter.Saxophone"/>
- <bean id="kenny" class="com.spring.test.setter.Instrumentalist" autowire="byName">
- <property name="song" value="Jingle Bells" />
- <property name="age" value="" />
- </bean>
- </beans>
采用byType的配置文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="test2" class="com.spring.test.setter.Saxophone"/>
- <bean id="test1" class="com.spring.test.setter.Saxophone" primary="true"/> <!-- 默认是false -->
- <bean id="kenny" class="com.spring.test.setter.Instrumentalist" autowire="byType">
- <property name="song" value="Jingle Bells" />
- <property name="age" value="" />
- </bean>
- </beans>
如果有多个类型匹配的bean,则可以采用 primary 来设置主要装配的bean,默认情况下是false。
【Spring实战】—— 8 自动装配的更多相关文章
- Spring(六)之自动装配
一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者 ...
- Spring 由构造函数自动装配
Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...
- 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理
最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...
- Spring学习笔记--自动装配Bean属性
Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...
- Spring基于注解自动装配
前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Spring实战3:装配bean的进阶知识
主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...
- Spring实战2:装配bean—依赖注入的本质
主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...
- Spring 学习——Spring注解——Autowiring(自动装配)
装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...
- spring bean autowire自动装配
转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...
随机推荐
- HDU6333 莫队+组合数
题目大意: 给定n m 在n个数中最多选择m个的所有方案 #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3 ...
- 鼠标拖动div,div跟随鼠标移动效果
<div id="boxDiv" style='width:20px;height:20px;position:absolute;background:red;'> ...
- PHP报错
php.ini ; 错误日志 log_errors = On ; 显示错误 display_errors = Off ; 日志路径 error_log = "/usr/local/lnmp/ ...
- pageHelper 分页插件使用
第一步:引入pageHelper的jar包. 链接:https://pan.baidu.com/s/1m3EyAmd_eoAsay0aM7uEkA 提取码:xmfi 第二步:需要在SqlMapConf ...
- C++ GUI Qt4编程(03)-1.3layout
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:layout.cpp #include <QApplication> #i ...
- RESTful 设计工具和Web框架
搭建开发环境几乎都搭建失败,因为需要FQ Spring Boot 和 Spring MVC 单独 Jersey官网可以直接访问 https://jersey.java.net/documentatio ...
- 详解http之post
详解http之post 首先,我们先看看jquery中的post方法的使用: $.ajax({ url:'api/bbg/goods/get_goods_list_wechat', data:{ , ...
- js动画实现&&回调地狱&&promise
1. js实现动画 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- java中创建User Libray
第一步:右键项目==>Build Path ==>Configure Build Path... 第二步:选择Libraries==>点击 Add Library.. 第三步:选择U ...
- android 学习资源网址
脚本之家: http://www.jb51.net/list/list_233_2.htm csdn: http://blog.csdn.net/xubo578/article/details/571 ...