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

一、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