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

spring中的配置一共也就这几个
- description描述不太重要,
 - bean在之前已经见识过了,
 - alias给bean起别名,
 - import在当前xml文件中导入其他xml文件
 
一、别名
在spring中别名主要是给bean的id起一个别名,同样也有好几种方式。
1、alias 配置
<alias name="user" alias="u"/>
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;
}(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>(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);
    }
(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>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;
}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>李四负责 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;
}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>总的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>使用的时候,使用总的配置文件即可
 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);
    }
存在问题
同时使用import还存在几个问题 导入bean 的id冲突
如果导入的文件中有多个重名id相同的bean
如果总配置文件中有取这个bean
如果在导入的xml文件中,因为导入的时候id相同的bean会不断覆盖,同名的bean后面的xml会覆盖前面的 xml,所以最后取的是最后导入这个id的xml文件中的bean
总结
与主配置中的id重名,调用主配置中的id;
多个import中配置中的id重名,调用最后import中配置中的id重名,即后面的覆盖前面的;
到此这篇关于Spring深入了解常用配置应用的文章就介绍到这了,更多相关Spring常用配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
