• 用XML的方式装配Bean


    Java知识点总结:想看的可以从这里进入

    8、用XML装配Bean


    在Spring中常常使用 XML 文件来注册管理Bean对象的依赖关系,它是通过先定义,然后初始化和依赖注入的。Spring在启动时,就会查找程序中的XML文件,读取其中配置信息,根据配置信息完成一系列的操作。

    Spring中的XML配置通常会根据内容起不同的名字,比如:spring-common.xml:用来存放通用的配置、spring-mybatis.xml:用来存放数据库相关的配置、spring-shiro.xml:用来存放shiro相关的配置。其中applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

    8.1、XML文件主体

    使用 beans 作为根标签(pom.xml使用project作为根标签,web.xml使用web-app作为根标签)

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    8.2、配置Bean对象

    8.2.1、bean标签

    在 XML 文件中通过标签 < bean> < /bean> 定义一个Bean对象。

    
    <bean id="" name="" class="" scope="" init-method="" destroy-method="">
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    8.2.2、属性注入

    Bean 属性注入,就是将属性注入到 Bean 中的过程,既可以注入普通属性,也可以注入一个对象(Bean)。

    Spring 主要通过以下 2 种方式实现属性注入:

    • 通过构造函数注入属性

      
      <constructor-arg name="" value="" index="" ref=""/>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    • setter方法 注入

      
      <property name="" value="" ref=""/>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    <bean id="" class="com.******" scope="" init-method="" destroy-method="">
        
        <constructor-arg name="属性名" value=""/>	
        <constructor-arg index="" ref="引用对象"/>	index是构造参数的顺序,从0开始,
    
        
        <property name="属性名" value=""/> 
        <property name="属性名" ref="注入一个bean对象,这里写id值"/>  引用对象
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    两个类:

    image-20230328192608792

    然后通过XML配置对象

    image-20230328193926480

    image-20230328194245560

    如果赋null值,不能直接 < property name=“” value=“null” /> 这样写,这样赋值赋的是null字符串,应该写成 :

    < property name=“name”>
    < null />
    < /property>

    8.2.3、注入内部bean

    在一个bean标签中再声明一个bean标签,这就是内部bean,假设一个bean中需要引用其他的对象时,之前我们是通过property、constructor-arg标签的 ref 属性来引入一个bean对象,而现在是直接在标签内创建bean标签:

    image-20230328200335085

    image-20230328200409706

    8.2.4、注入其他属性

    除了内部bean外,我们还可以在 property、constructor-arg 便签内部中,配置 Java 集合类型的属性和参数(如 List、Set、Map、Properties 等等)。

    标签描述
    list注入一个list类型的值,允许重复
    array注入一个数据类型的值
    set注入set类型的值,不可重复
    map用于注入 key-value 的集合,其中 key 和 value 都可以是任意类型
    props用于注入 key-value 的集合,其中 key 和 value 都是字符串类型
    null注入空值
    <bean id="" class="">
        
        <property name="属性名">
            <array>
                <value>value>
                <ref bean="bean的id值"/>	注入的是bean对象
            array>
        property>
        
        
        <property name="属性名">
            <list>
                <value>value>
                <ref bean="对象id值"/>	注入的是对象
            list>
        property>
        
        
        <property name="属性名">
            <set>
                <value>value>
                <ref bean="对象id值"/>	注入的是对象
            set>
        property>
        
        
        <property name="属性名">
            <map>
                <entry key="" value=""/>
                <entry key-ref="" value-ref=""/>
            map>
        property>
        
        
        <property name="属性名">
            <props>		
                <prop key="">prop>
                <prop key=""/>
            props>
        property>
        
        
    	<property name="属性名">
            <null/>			赋空值
        property>
           
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    image-20230328203026645

    image-20230328203300613

    8.3、命名空间装配

    Spring使用XML进行属性装配其实并不是很复杂,不过XML里有命名空间这个概念,所以Spring也提供了 命名空间p和c 装配的方式。(p就是set方法注入,c就是构造方法注入)。除了这个外,还可以使用 util 注入集合等属性

    命名空间装配可以减少标签的使用

    如果你想使用命名空间p、c、和util,只需要在Spring 的XML 头文件中加入约束文件:

    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    
    • 1
    • 2
    • 3
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        
        <bean id="person1" class="com.yu.pojo.Person" c:_0="" c:_1="" c:_2=""/>
        
        
        <bean id="person2" class="com.yu.pojo.Person" p:name="" p:sex="" p:age=""/>
        
        
        <util:list id="list">
            <ref bean="p"/>     	
            <value>Stringvalue>    
        util:list>
    
        <util:set id="set">
            <ref bean="p"/>
            <value>Stringvalue>
        util:set>
    
        <util:map id="map">
            <entry key-ref="p" value-ref="p"/>
        util:map>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    image-20230328211150152

    image-20230328211218602

    8.4、导入其他XML

    可以多配置一些xml文件,方便区分,然后用主文件引入这些xml文件

    
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    	
    	<import resource="***.xml"/>
        <import resource="***.xml"/>
        .......
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    比如我们创建一个application.xml文件,然后在其中引入 beans.xml文件,引入后,我们可以通过获取application文件,就能获取到 beans 中定义的bean

    image-20230328211411466

    image-20230328211522242

    8.5、自动装配

    Spring提供了自动装配的功能,在不使用 constructor-arg 和 property 手动装配的情况下也可以进行自动装配bean。(自动装配不能用在字面量的属性上,只能用在我们自定义的类)

    Spring 的自动装配功能可以让 Spring 容器依据自动装配的规则(有五种),为指定的 Bean 从应用的上下文中查找它所依赖的 Bean,并自动建立 Bean 之间的依赖关系。

    1. byName:按名字自动装配,bean的 id名或 name 名必须和类中的属性名要一致才能自动装配
    2. byType:按类型自动装配,在容器上下文中自动寻找类型相同的bean(对象类型要全局唯一),byType自动装配时id可省略
    3. constructor:是根据构造器参数的数据类型,进行 byType 模式的自动装配。
    4. default:默认采用上一级元素 beans 设置的自动装配规则(default-autowire)进行装配。
    5. no:默认的情况,不自动装配,需要手动装配

    自动装配功能能够有效地简化 Spring 应用的 XML 配置,因此在配置数量多时采用自动装配可以降低工作量。Spring 框架式默认使不支持自动装配的,要想使用自动装配,则需要对 Spring XML 配置文件中 bean 元素的 autowire 属性进行设置。

    
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    	
        <bean id="p1" class="****">bean>
        <bean id="p2" class="****">bean>
        
        <bean id="" class="">
        	<property name="p1" ref="p1"/>
            <property name="p2" ref="p2"/>
        bean>
         
        
    	<bean id="" class="" autowire="byName"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    image-20230328212126054

    image-20230328212147238

  • 相关阅读:
    论文笔记 - PRISM: A Rich Class of Parameterized Submodular Information Measures for Guided Subset Selection
    口袋参谋:如何挑选淘宝热词?这一招很重要!
    【php实战项目训练】——thinkPhP的登录与退出功能的实现,让登录退出畅通无阻
    Linux基本指令一
    地球系统模式(CESM)技术应用
    聊聊FLINK-25631贡献
    js 作用域理解(查找变量)
    地方院校C语言程序设计课程建设与实践
    元宇宙产业委调研行杭州站 | 联合西溪谷管委会共商元宇宙赋能实体经济
    初识Load Runner
  • 原文地址:https://blog.csdn.net/yuandfeng/article/details/126798161