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 配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM