Spring_总结_03_装配Bean(三)之XML配置

简介: 一、前言 本文承接上一节:Spring_总结_03_装配Bean(二)之Java配置 上一节说到,当需要显示配置时,首选类型安全并且比XML更强大Java配置。 那什么时候使用XML配置呢? (1)维护已有XML配置 (2)想使用便利的XML命名空间,并且在JavaConfig中没有同样的实现。

一、前言

本文承接上一节:Spring_总结_03_装配Bean(二)之Java配置

上一节说到,当需要显示配置时,首选类型安全并且比XML更强大Java配置。

那什么时候使用XML配置呢?

(1)维护已有XML配置

(2)想使用便利的XML命名空间,并且在JavaConfig中没有同样的实现。

 

二、引入schema

<?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">


</beans>
View Code

用来装配bean的最基本的XML元素包含在spring-beans模式中,在上面这个XML文件中,它被定义为根命名空间。

<beans>是该模式中的一个元素,它是所有Spring配置文件的根元素。

 

三、声明Bean

    <bean id="compactDisc" class="soundsystem.SgtPeppers"></bean>

id :指定bean的名字,以便于被其他bean引用。

class : 全类名,指定创建bean的Class

 

注:

(1)通过xml配置的方式,我们不再直接负责创SgtPeppers,而在JavaConfig中,我们却是需要这样做的。当Spring发现 <bean>元素时,他会调用默认构造器来创建bean。

(2)将bean的类型以字符串的形式设置在class属性中,不便于重构。(如修改类名时)

 

 

四、构造器注入

注入bean有两种方式:

(1)构造器注入

(2)属性注入

 

这两种方式如何选择:强依赖选择构造器注入,可选性依赖使用属性注入。

 

1. constructor-arg

<?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="compactDisc" class="soundsystem.SgtPeppers"></bean>

    <bean id="cdPlayer"    class="soundsystem.CDPlayer">
        <constructor-arg ref="compactDisc" />
    </bean>
    
</beans>
View Code

 

2. c-命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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="compactDisc" class="soundsystem.SgtPeppers"></bean>
  
    <bean id="cdPlayer" class="soundsystem.CDPlayer"
          c:cd-ref="compactDisc"/>
    
    <bean id="cdPlayer2" class="soundsystem.CDPlayer"
          c:_0-ref="compactDisc"/>


</beans>
View Code

(1)构造器参数名

"c:"为命名空间

“-ref”表示注入引用

“cd”为构造器参数名

 

(2)构造器参数索引

“_i”为第i个参数。

表示为第i个参数注入bean

 

3. 字面量

将字面量注入到构造器中

(1)constructor-arg

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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="blankDisc" class="soundsystem.BlankDisc">
        <constructor-arg value="字面量"/>
    </bean>



</beans>
View Code

 

(2)构造器参数名

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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="blankDisc" class="soundsystem.BlankDisc"
          c:_title="标题"
          c:_author="作者" />

</beans>
View Code

 

(3)构造器参数索引

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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="blankDisc" class="soundsystem.BlankDisc"
          c:_0="标题"
          c:_1="作者" />

</beans>
View Code

 

4. 集合

BlankDisc

public class BlankDisc  implements CompactDisc{
    private String title;
    private String author;
    private List<String> tracks;

    public BlankDisc(String title, String author, List<String> tracks) {
        this.title = title;
        this.author = author;
        this.tracks = tracks;
    }

    public void play(){
        System.out.print("aaaaaaaa");
    }
}
View Code

 

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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="compactDisc" class="soundsystem.BlankDisc">
        <constructor-arg value="标题"/>
        <constructor-arg value="作者"/>
        <constructor-arg>
            <list>
                <value>111111111</value>
                <value>222222222</value>
                <value>333333333</value>
                <value>444444444</value>
                <value>555555555</value>
            </list>
        </constructor-arg>
    </bean>

    <bean id="compactDisc2" class="soundsystem.BlankDisc">
        <constructor-arg value="标题"/>
        <constructor-arg value="作者"/>
        <constructor-arg>
            <list>
                <ref bean="compactDisc" />
                <ref bean="compactDisc" />
                <ref bean="compactDisc" />
                <ref bean="compactDisc" />
            </list>
        </constructor-arg>
    </bean>


    <bean id="compactDisc3" class="soundsystem.BlankDisc">
        <constructor-arg value="标题"/>
        <constructor-arg value="作者"/>
        <constructor-arg>
            <set>
                <value>111111111</value>
                <value>222222222</value>
                <value>333333333</value>
                <value>444444444</value>
                <value>555555555</value>
            </set>
        </constructor-arg>
    </bean>

</beans>
View Code

 

五、属性注入

1.注入引用

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="compactDisc" class="soundsystem.CompactDisc" />

    <bean id="cdPlayer" class="soundsystem.CDPalyer" >
        <property name="compactDisc" ref="compactDisc" />
    </bean>

    <bean id="cdPlayer2" class="soundsystem.CDPalyer"
          p:compactDisc-ref="compactDisc"/>

</beans>
View Code

 

2.注入字面量

BlankDisc

public class BlankDisc  implements CompactDisc{
    private String title;
    private String author;
    private List<String> tracks;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setTracks(List<String> tracks) {
        this.tracks = tracks;
    }

    public void play(){
        System.out.print("aaaaaaaa");
    }
}
View Code

 

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/plugin"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd">

    <!--1.空属性-->
    <bean id="reallyBlankDisc" class="soundsystem.BlankDisc" />

    <!--2.注入list-->
    <bean id="compactBlankDisc" class="soundsystem.BlankDisc" >
        <property name="title" value="标题"/>
        <property name="author" value="作者"/>
        <property name="tracks" >
            <list>
                <value>11111111</value>
                <value>11111111</value>
                <value>11111111</value>
                <value>11111111</value>
            </list>
        </property>

    </bean>


    <!--3.p命名空间装配引用与装配字面量的唯一区别在于是否带有 -ref
          p命名空间不能用来装配集合
    -->
    <bean id="compactBlankDisc2" class="soundsystem.BlankDisc" 
          p:title="标题"
          p:author="作者">
        <property name="tracks" >
            <list>
                <value>11111111</value>
                <value>11111111</value>
                <value>11111111</value>
                <value>11111111</value>
            </list>
        </property>
    </bean>

    <!--4.<util:list>会创建一个列表bean -->
    <util:list id="tracklist">
        <value>1111111111</value>
        <value>1111111111</value>
        <value>1111111111</value>
        <value>1111111111</value>
    </util:list>
    <bean id="compactBlankDisc3" class="soundsystem.BlankDisc"
          p:title="标题"
          p:author="作者"
          p:tracks-ref="tracklist" />
</beans>
View Code

 

目录
相关文章
|
6月前
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
1311 1
|
6月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
211 0
|
9月前
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
182 69
|
9月前
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
9月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
216 6
|
12月前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
595 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
11月前
|
XML Java 数据格式
手动开发-简单的Spring基于XML配置的程序--源码解析
手动开发-简单的Spring基于XML配置的程序--源码解析
183 0
|
XML Java 数据格式
Spring高手之路18——从XML配置角度理解Spring AOP
本文是全面解析面向切面编程的实践指南。通过深入讲解切面、连接点、通知等关键概念,以及通过XML配置实现Spring AOP的步骤。
270 6
Spring高手之路18——从XML配置角度理解Spring AOP
|
XML 前端开发 Java
Spring xml配置
Spring xml配置
|
XML Java 数据格式
Spring IOC—基于XML配置和管理Bean 万字详解(通俗易懂)
Spring 第二节 IOC—基于XML配置和管理Bean 万字详解!。
911 5