自动装备

1、定义

  • 自动装配(autowiring): 将某个Bean实例中所引用的其它的Bean自动注入到当前Bean实例中
  • 自动装配就是指由Spring来自动地注入依赖对象,无需人工参与。
  • 自动装配的好处是减少构造器注入和setter注入配置,减少配置文件的长度。自动装配通过配置<bean>标签的 “autowire”属性来改变自动装配方式。

2、四种装配方式

  • no(default): 默认值,即不启动自动装配,需要显示地引用相应的bean

    setter 注入 : ( setter-base ),提供setter方法进行注入,依赖于无参构造和setter方法

    Person类

    package ecut.ioc.autowiring;
    
    public class Person {
    
        private Integer id;
    private String name; private Dog wangcai ; public Person() {
    super();
    } public Person(Dog wangcai) {
    super();
    this.wangcai = wangcai;
    System.out.println( "public Person(Dog)" );
    } public Integer getId() {
    return id;
    } public void setId(Integer id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Dog getWangcai() {
    return wangcai;
    } public void setWangcai(Dog wangcai) {
    this.wangcai = wangcai;
    } }

    Dog类

    package ecut.ioc.autowiring;
    
    public class Dog {
    
        private String name ;
    
        public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } }

    配置文件

    <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean> <bean id="p" class="ecut.ioc.autowiring.Person" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    <property name="wangcai" ref="dog" />
    </bean>

    构造方法注入 : ( constructor-base )通过构造方法往里面传入值

    配置文件

    <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean> <bean id="p" class="ecut.ioc.autowiring.Person" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    <constructor-arg name="wangcai" ref="dog" />
    </bean>
  • byName: 根据属性名称和被引用的bean名称来实现自动注入(setter)

    byName-autowiring.xml

    <?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-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean> <bean id="wangcai" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="旺财" />
    </bean> <!-- 根据 名称 实现 自动装配 ( setter ) -->
    <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="byName" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    <!-- <property name="wangcai" ref="wangcai" /> -->
    </bean> </beans>

    如果在被装配的bean中含有xxx属性(实际上是setter方法),则会自动把id为xxx的bean装配上来
    测试类

    package ecut.ioc.autowiring;
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireByName { public static void main(String[] args) { String configLocations = "classpath:ecut/**/byName-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Person s = container.getBean( "huaan" , Person.class ); System.out.println( s.getId() + " : " + s.getName() ); Dog d = s.getWangcai(); System.out.println( d.getName() ); container.close(); } }
  • byType: 根据属性类型和被引用的bean的类型来实现自动注入(setter)

    byType-autowiring.xml

    <?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-4.3.xsd"> <!-- NoUniqueBeanDefinitionException
    <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean>
    --> <bean id="wangcai" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="旺财" />
    </bean> <!-- 根据 类型 实现 自动装配 ( setter ) -->
    <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="byType" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    </bean> </beans>

    如果在被装配的bean中含有一个xxx类型的属性,则会自动把类型为xxx的bean装配上来,但是如果含有多个xxx类型的bean,则抛出NoUniqueBeanDefinitionException
     测试案例

    package ecut.ioc.autowiring;
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireByType { public static void main(String[] args) { String configLocations = "classpath:ecut/**/byType-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Person s = container.getBean( "huaan" , Person.class ); System.out.println( s.getId() + " : " + s.getName() ); Dog d = s.getWangcai(); System.out.println( d.getName() ); container.close(); } }
  • constructor: 根据构造方法的参数类型和被引用的Bean的类型实现自动装配(constructor)

    当存在多个同种类型的Bean与构造方法中的参数类型相同时:
    如果某Bean的名称跟参数的名称一致,则根据名称进行自动装配。constructor-autowiring.xml中bean的名称和构造方法中 public Person(Dog wangcai) 的参数保持一致。下面配置输出狗的名称是旺财

    <?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-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean> <bean id="wangcai" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="旺财" />
    </bean> <!-- 根据 类型 实现 自动装配 ( constructor ) -->
    <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="constructor" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    </bean> </beans>

    如果这些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-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean> <bean id="wc" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="旺财" />
    </bean> <!-- 根据 类型 实现 自动装配 ( constructor ) -->
    <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="constructor" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    </bean> </beans>

    当且仅当与构造方法中的参数类型相同的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-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" >
    <property name="name" value="拉的多不多" />
    </bean> <!-- 根据 类型 实现 自动装配 ( constructor ) -->
    <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="constructor" >
    <property name="id" value="1001" />
    <property name="name" value="华安" />
    </bean> </beans>

    测试类

    package ecut.ioc.autowiring;
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireByConstructor { public static void main(String[] args) { String configLocations = "classpath:ecut/**/constructor-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Person s = container.getBean( "huaan" , Person.class ); System.out.println( s.getId() + " : " + s.getName() ); Dog d = s.getWangcai(); System.out.println( d.getName() ); container.close(); } }

    使用构造方法自动装备,创建person实例时,使用带参数的构造,并为参数注入指定类型的bean。

加载配置文件通配符

1、* 匹配同一级别路劲的多个字符,ecut/*/autowiring/constructor-autowiring.xml
2、 ** 匹配多级路劲中的多个字符,ecut/**/constructor-autowiring.xml

String configLocations = "classpath:ecut/**/constructor-autowiring.xml" ;
AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );

ecut.ioc.autowiring包中有一个constructor-autowiring.xml
3、 ?仅匹配一个字符

4、如果多个包中都beans.xml文件,并且期望全部加载它们,则可以写作classpath*:ecut/**/beans.xml

ecut包中有beans.xml, ecut.ex包中有beans.xml,ecut.xxx.yyyy包中有beans.xml

转载请于明显处标明出处

https://www.cnblogs.com/AmyZheng/p/9253193.html

Spring学习(五)的更多相关文章

  1. spring学习(五) ———— 整合web项目(SSM)

    一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后在编写springmvc. 1.2.开发需求 查询商品列表(从数据库中查询) 1.3.创建web工程 ...

  2. Spring学习五(JDBC支持)

    Spring的jdbc支持 1配置db.properties,将有关JDBC的信息载入 2bean文件配置数据源,这里用e3p0作为数据源载入db.properties 3配置template的bea ...

  3. Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)

    在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...

  4. Spring学习五

    1: servlet生命周期:  Servlet加载    ->   实例化->   服务 ->  销毁 2:Servlet重要函数: init():在Servlet的生命周期中,仅 ...

  5. Spring学习五----------Bean的配置之Bean的生命周期

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的生命周期 1.定义 2.初始化 3.使用 4.销毁 初始化和销毁的三种方式 1.实现org.springframework.beans.fa ...

  6. Spring学习(五)--构建Spring Web应用程序

    一.Spring MVC起步 看过猫和老鼠的小伙伴都可以想象Tom猫所制作的捕鼠器:它的目标 是发送一个小钢球,让它经过一系列稀奇古怪的装置,最后触发捕鼠 器.小钢球穿过各种复杂的配件,从一个斜坡上滚 ...

  7. spring学习五:Spring Bean 定义继承

    Bean 定义继承 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置数据.子定义可以根据需要 ...

  8. spring学习 五 依赖注入的方式

    依赖注入有两种方式: 1 构造注入,如果<bean>标签下使用<contructor-arg>,则是构造注入 2 setter注入,就是调用setter方法注入,如果<b ...

  9. spring 学习(五):spring 事务

    spring 学习(五):spring 事务 事务概要 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: 为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同 ...

随机推荐

  1. Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向

    一.shell介绍 shell脚本是日常Linux系统管理工作中必不可少的,不会shell,就不是一个合格管理员. shell是系统跟计算机硬件交互使用的中间介质,一个系统工具.实际上在shell和计 ...

  2. Python学习之Craps赌博游戏篇

    在此先安利一波大佬的Python学习项目地址:https://github.com/jackfrued/Python-100-Days 这些天一直在看着大佬的项目学习Python,这是第五天循环学习完 ...

  3. Travel in desert

    传送门 不算难吧 应该有思路的 还是太水了吧 (而且和货车运输很像的啊 ---------------------------------------------------------------- ...

  4. PP Bottle Have High Cycle Times

    This year, the participation of 0.1% -0.4% sorbitol nucleating agent in general PP can produce high- ...

  5. 高级命令之awk

    1.提取文件内容 2.提取ip

  6. 关于package.json和package-lock.json的区别

    package.json文件记录你项目中所需要的所有模块.当你执行npm install的时候,node会先从package.json文件中读取所有dependencies信息,然后根据depende ...

  7. python-turtle-画雪花-2种方法及效果的详解

    1.方法一: 代码: #python3.8 #xuguojun #2020.1.30 #导出模块 import turtle as t import random as r #定义画雪 def dra ...

  8. Bug搬运工-CSCvn51483:AP datasheet should contain info if the AP contains or not temperature sensor

    目前在老一些的思科AP上,应该都没有温度传感器的功能(例如AP1600,wave 1 AP1700...),但是在思科官方没有明确的文档去说明这个问题. 在Outdoor AP(室外AP,例如AP15 ...

  9. AppBar 自定义顶部导航按钮 图标、颜色 以及 TabBar 定义顶部 Tab 切换

    一.Flutter AppBar 自定义顶部按钮图标.颜色 leading   在标题前面显示的一个控件,在首页通常显示应用的 logo:在其他界面通常显示为返回按钮 title  标题,通常显示为当 ...

  10. 计算机二级-C语言-对二维数组数据进行处理。对文件进行数据输入。形参与实参。

    //函数fun的功能为:计算x所指数组中N个数的平均值(规定所有数都为正数),平均值通过形参返回给主函数,将小于平均值且最接近平均值的数作为函数值返回,并输出. //重难点:形参与实参之间,是否进行了 ...