Spring_02_配置文件_import和name

前文:Spring_概述与Hello world

 

import

为方便管理配置文件,推荐使用import来规划配置文件。

在applicationContext.xml中,通过配置<import>的resource来导入配置文件。

1、默认情况下,是使用相对路径来寻找配置文件。

(相对于applicationContext.xml)

2、Spring提供了前缀标记用于辅助查找配置文件。

[file:]:使用文件系统的路径方式查找。

[classpath:]:从classpath后查找。推荐使用该方式。

  • 演示

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
http://www.springframework.org/schema/beans/spring-beans.xsd">
    
   <import resource="classpath:com/hanaii/hello/hello.xml"/>
    
</beans>

hello.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="hello" class="com.hanaii.hello.Hello" />
</beans>

 

name

Spring配置文件中,要求<bean>中的id属性值是唯一的。

若bean需要其他的名字,可以配置name属性。

1、name属性可通过逗号、分号、空格来分割多个名字。

2、可通过BeanFactory的getAlias方法获取一个bean的所有名字。

3、一般使用id即可。在SpringMVC,可在name中配置多个名字来代表映射的URL地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值