SpringMVC与SpringBoot配置文件的加载区别

本文详细对比了SpringMVC与SpringBoot的配置方式,SpringMVC通过在web.xml中配置加载位于classpath下的配置文件,而SpringBoot则默认加载位于classpath下的配置文件,无需额外配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、SpringMVC

配置文件在classpath下。

在web.xml中配置加载。

以下项目为示例

其中引用关系为

1. applicationContext-dao.xml 引用了mybatis文件夹中的配置文件

2. applicationContext-shiro.xml 引用了shiro文件夹中的配置文件

3. springmvc.xml 引用了resource文件夹中的配置文件

4. web.xml 引用了spring文件夹中的配置文件

<!-- web.xml加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!-- 加载log4j -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
<!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>SSM</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

applicationContext-dao.xml

<!-- 数据库连接池 -->
    <!-- 加载配置文件 -->
    <!-- 
        由db.properties改为*.properties, 这样也会加载其他的属性文件 可以使用 @Value
        由于父子容器关系,service层(父)的属性文件在springmvc层(子)是读取不到的,子只能读取对象,属性是读取不到的
     -->
    <context:property-placeholder location="classpath:resource/*.properties" />
    <!-- 配置sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置扫描包,加载mapper代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.suen.mapper"></property>
    </bean>

applicationContext-shiro.xml

<!-- 用户授权信息Cache, 采用EhCache -->
    <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"/>
    </bean>

 

 

二、SpringBoot:

不需要配置,默认加载。

配置文件在classpath下。

例如:

application.properties是默认加载的

而application.properties 引用了i18n、mapper、static、templates文件夹中的配置文件

只要是.yml或者.properties。

示例application.properties中的mybatis 配置

 

转载于:https://www.cnblogs.com/suenshuai/p/9796450.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值