首页 > 编程语言 > 详细

【Spring】三、Spring配置

时间:2021-07-31 00:43:11      阅读:35      评论:0      收藏:0      [点我收藏+]

5、Spring配置

5.1、别名

使用场景

  1. 原本的bean名,因为遵循命名规范而起的比较长;
  2. 协同开发。

5.1.1、alias标签

  • name:参考bean,即要起别名的bean;
  • alias:为bean起的别名。
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
    <constructor-arg name="id" value="7"/>
    <constructor-arg name="name" value="jaywee"/>
</bean>

<alias name="hello" alias="hello1"/>
<!-- 还可以为别名起别名,相当于多个别名 -->
<alias name="hello1" alias="hello2"/>

5.1.2、bean标签的name属性

  • name:表示别名,可以起多个别名;
  • 多个别名之间用【空格/逗号/分号】分割
<bean id="hello" class="indi.jaywee.pojo.HelloSpring" name="hello3 hello4,hello5;hello6">
    <constructor-arg name="id" value="7"/>
    <constructor-arg name="name" value="jaywee"/>
</bean>

5.2、bean

常用配置

  1. id:bean的唯一标识符,代表对象名;
  2. class:bean的全限类名;
  3. name:别名;

5.3、import

用于协同开发, 可以将多个配置文件,导入合并为一个。

  • 在实例化容器时,可以传入多个参数。

    // 实例化容器
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml", "beans1.xml", "beans2.xml");
    
  • 通过import标签,将多个配置文件合并成为一个总的配置文件(applicationContext.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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <import resource="beans.xml"/>
        <import resource="beans1.xml"/> 
        <import resource="beans2.xml"/>  
        
    </beans>
    
  • 在实例化容器时,只需要传入applicationContext.xml即可

    // 实例化容器
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    

【Spring】三、Spring配置

原文:https://www.cnblogs.com/secretmrj/p/15082788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!