• Spring 框架学习(四)---- 常用配置


    Spring 框架学习(四)---- 常用配置


    现在这里简单了解一下spring 配置文件中的一些常用配置,在后面我们还会遇到更多的配置,在后文继续进行介绍了。

    在这里插入图片描述

    spring中的配置一共也就这几个

    • description描述不太重要,

    • bean在之前已经见识过了,

    • alias给bean起别名,

    • import在当前xml文件中导入其他xml文件


    一、 别名


    在spring中别名主要是给bean的id起一个别名,同样也有好几种方式。


    1、alias 配置

       <alias name="user" alias="u"/>
    
    • 1

    alias是给bean的id起别名

    • name 是bean的id

    • alias 是bean的别名

    (1)先定义普通实体类

    package com.kuang.pojo;
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
    
    @Getter
    @Setter
    @ToString
    public class User {
        private int id;
        private String userName;
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (2)在配置文件中装配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">
    
    
       <alias name="user" alias="u"/>
    
       <bean id="user" class="com.kuang.pojo.User">
           <property name="id" value="1"/>
           <property name="userName" value="root"/>
           <property name="password" value="123456"/>
       </bean>
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    (3)通过别名也能拿到装配的bean

        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            User user = context.getBean("u",User.class);
            System.out.println(user);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (4)查看运行结果

    在这里插入图片描述


    二、bean 的配置


    也可以通过bean来配置别名,而且可以给一个bean 配置多个别名

       <bean id="user" class="com.kuang.pojo.User" name="u1,u2,u3,u4">
           <property name="id" value="1"/>
           <property name="userName" value="root"/>
           <property name="password" value="123456"/>
       </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    name就是给当前bean配置别名,可以多个别名写在一起,中间使用空格/逗号/分号进行分割,spring都能识别


    三、import


    在团队开发使用中,还是非常常见的。它可以将多个配置文件,导入合成一个

    假设一个团队中有多个人进行开发,这三个人负责不同类的开发,不同的类需要注册到不同的bean中

    • 张三 beans1.xml

    • 李四 beans2.xml

    • 王五 beans3.xml

    我们可以利用import 将所有人的beans.xml合并成一个总的ApplicationContext.xml ,最后使用的时候使用总的配置文件即可。

    张三负责 User类 以及注册到bean1.xml文件中

    User类

    package com.kuang.pojo;
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
    
    @Getter
    @Setter
    @ToString
    public class User {
        private int id;
        private String userName;
        private String password;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    bean1.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.xsd">
    
        <bean id="user" class="com.kuang.pojo.User">
            <property name="id" value="1"/>
            <property name="userName" value="root"/>
            <property name="password" value="123456"/>
        </bean>
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    李四负责 Student类,bean2.xml

    Stduent 类

    package com.kuang.pojo;
    
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
    
    @Setter
    @Getter
    @ToString
    public class Student {
        private int id;
        private String name;
        private String sex;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    bean2.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.xsd">
    
        <bean id="student" class="com.kuang.pojo.Student">
            <property name="id" value="1"/>
            <property name="name" value="张三"/>
            <property name="sex" value=""/>
        </bean>
    
    </beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    总的ApplicationContext.xml配置文件,导入了bean1.xml 和 bean2.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.xsd">
    
     <import resource="bean1.xml"/>
     <import resource="bean2.xml"/>
    
    </beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    使用的时候,使用总的配置文件即可

     public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            User user = context.getBean("user",User.class);
            Student student = context.getBean("student",Student.class);
            System.out.println(user);
            System.out.println(student);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (1)存在问题


    同时使用import还存在几个问题 导入bean 的id冲突

    如果导入的文件中有多个重名id相同的bean

    • 如果总配置文件中有取这个bean,

    • 如果在导入的xml文件中,因为导入的时候id相同的bean会不断覆盖,同名的bean后面的xml会覆盖前面的 xml,所以最后取的是最后导入这个id的xml文件中的bean


    (2)总结


    与主配置中的id重名,调用主配置中的id;

    多个import中配置中的id重名,调用最后import中配置中的id重名,即后面的覆盖前面的;

  • 相关阅读:
    【Linux详解】——进程概念
    Oracle Error FRM-40203: Field must be entered completely
    【初学者入门C语言】之编译预处理(十)
    Kubernetes学习笔记-保障集群内节点和网络安全20220827
    konga+prometheus监控服务
    「AI工程师」数据处理与分析-工作指导
    Blazor入门100天 : 自做一个支持长按事件的按钮组件
    VB.NET—DataGridView控件教程详解
    基于记忆与模型协同过滤的电影推荐系统研究与实践(文末送书)
    搞懂 Dubbo 入门理论,这一篇就够
  • 原文地址:https://blog.csdn.net/rain67/article/details/125320446