Spring加載屬性文件方式(自動加載優(yōu)先級問題)
Spring加載屬性文件
方式1、用xml文件配置
正常情況下,spring整合mybatis的配置文件的dataSource部分如下
?<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ? ? ?? ?<property name="driverClassName" value="com.mysql.jdbc.Driver"></property> ? ? ?? ?<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property> ? ? ?? ?<property name="username" value="root"></property> ? ? ?? ?<property name="password" value="123456"></property> ? ? </bean>
可以將數(shù)據(jù)庫的鏈接信息寫到屬性文件中,如下。
jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.driver=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=123456
在spring配置文件中,就可以用${}的形式獲取屬性信息,但需要加入 <context:property-placeholder />標簽設置屬性文件的路徑。即
?<context:property-placeholder location="classpath:db.properties"/> ? ?
?<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
??? ?<property name="driverClassName" value="${jdbc.driver}"></property> ? ? ?
?? ?<property name="url" value="${jdbc.url}"></property>
?? ?<property name="username" value="${jdbc.username}"></property>
?? ?<property name="password" value="${jdbc.password}"/>
?</bean>但是由此會引發(fā)另一個問題,自動加載的優(yōu)先級特別高(就是先實例化)
若org.mybatis.spring.SqlSessionFactoryBean的id為sqlSessionFactory,當自動注入時,org.mybatis.spring.mapper.MapperScannerConfigurer類下的SqlSessionFactory屬性會自動注入,然后org.mybatis.spring.SqlSessionFactoryBean也會實例化,而org.mybatis.spring.SqlSessionFactoryBean中含有dateSourse,所以org.springframework.jdbc.datasource.DriverManagerDataSource也會實例化,但是這時屬性文件還沒有加載,造成程序出錯Error setting property values,總而言之就是在屬性文件加載之前,類實例化了,結果得不到屬性文件中的值。
解決辦法
第1步,更改org.mybatis.spring.SqlSessionFactoryBean的id名稱,例如factory
第2步,將org.mybatis.spring.mapper.MapperScannerConfigurer中加入<property name="sqlSessionFactoryBeanName" value="factory"></property>,如果用<property name="sqlSessionFactory/>標簽同樣出現(xiàn)以上的問題。
因為自動注入只影響ref的,而sqlSessionFactoryBeanName的值的類型時string,用value賦值,所以不受影響
以下是完整的spring整合mybatis的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? xmlns:aop="http://www.springframework.org/schema/aop"
? ? xmlns:context="http://www.springframework.org/schema/context"
? ? xsi:schemaLocation="http://www.springframework.org/schema/beans
? ? ? ? http://www.springframework.org/schema/beans/spring-beans.xsd
? ? ? ? http://www.springframework.org/schema/aop
? ? ? ? http://www.springframework.org/schema/aop/spring-aop.xsd
? ? ? ? http://www.springframework.org/schema/context
? ? ? ? http://www.springframework.org/schema/context/spring-context.xsd"?
? ? ? ? default-autowire="byName">
? ? ? ??
? ? ? <context:property-placeholder location="classpath:db.properties"/> ? ? ? ?
? ? ? ? <!-- 數(shù)據(jù)源封裝類,數(shù)據(jù)源:獲取數(shù)據(jù)庫連接,spring-jdbc.jar中 -->
? ? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
? ? ? ?? ?<property name="driverClassName" value="${jdbc.driver}"></property> ? ? ?
? ? ??? ?<property name="url" value="${jdbc.url}"></property>
? ? ??? ?<property name="username" value="${jdbc.username}"></property>
? ? ??? ?<property name="password" value="${jdbc.password}"/>
? ? ? </bean>
? ? ? ? <!-- 創(chuàng)建SqlSessionFactory對象 -->
? ? ? <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ?? ?<!-- 數(shù)據(jù)庫連接信息來源于dataSource -->
? ? ? ?? ?<!-- <property name="dataSource" ref="dataSource"></property> -->
? ? ? ?? ?<!-- 相當于mybatis中別名默認包 -->
? ? ? ?? ?<property name="typeAliasesPackage" value="com.lee.pojo"></property>
? ? ? </bean>
? ? ? <!-- 掃描器相當于mybatis設置接口綁定時xml的mappers下的package標簽 -->
? ? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ?? ?<!-- 掃描哪個包 -->
? ? ? ?? ?<property name="basePackage" value="com.lee.mapper"></property>
? ? ? ?? ?<!-- 和factory產(chǎn)生關系 -->?? ? ??
? ? ? <property name="sqlSessionFactoryBeanName" value="factory"></property>
? ? ? </bean> ? ??
</beans>方式2、用注解
使用注解方法時,需要添加標簽,這里的包名指的是含有注解的類所在包
<context:component-scan base-package="com.lee.service.impl"></context:component-scan>
測試的properties
my.value=hello
測試類
public class Demo{
?? ?@Value("${my.value}")
?? ?private String test;?? ?
}這樣就可以實例化Demo時給test注入值
對Spring加載順序的理解
web.xml初始化
- Web項目啟動的時候,容器(如:tomcat)讀取webapp/WEB-INF/web.xml文件,讀取和;
- 創(chuàng)建ServletContex,Web項目所有部分都可以使用該上下文ServletContex;
- 容器將解析為key-value對,并交給ServletContext;
- 容器根據(jù)中的類創(chuàng)建監(jiān)聽實例,即啟動監(jiān)聽;
- listener監(jiān)聽類中會contextInitialized(ServletContextEvent servletContextEvent)初始化方法,可通過ServletContextEvent.getServletContext().getInitParameter(“field”)獲得value的值;
- 解析,并啟動攔截器 攔截器開始起作用,當有請求進入時,執(zhí)行Filter的doFilter方法;
- 最后加載和初始化配置在load on startup的servlets;
- 加載Spring,如果filter需要用到bean,但加載順序是: 先加載filter 后加載spring,則filter中初始化操作中的bean為null.如果過濾器中要使用到 bean,可以將spring 的加載 改成Listener的方式:org.springframework.web.context.ContextLoaderListener
先創(chuàng)建上下文對象servletcontext,再加載監(jiān)聽器,然后去加載攔截器,最后加載servlet
路徑問題:Spring MVC靜態(tài)資源攔截(No mapping found for HTTP request with URI in DispatcherServlet with name ’ ')問題

/ 是加載視圖配置的目錄下的文件,前提是webapp下沒有默認文件;如果有文件就訪問默認文件
/* 我的測試是都報404
spring加載流程
啟動先加載web.xml(包含:加載applicationContext.xml、listener:contextloadlistener、:DispatcherServlet),通過applicationContext.xml加載接口及java實現(xiàn)類、加載config.properties文件、加載數(shù)據(jù)庫驅(qū)動等、加載mybatis.config文件(SqlSessionFactoryBean:加載xml文件)、加載數(shù)據(jù)庫的接口和mapper.xml、加載springmvc視圖等。
要保證install后mapper.java、mapper.xml要在同一文件下
如果用EL表達式(ModelAndView)時表達式出現(xiàn)問題解決如下:(搜索:SpringMVC中JSP頁面不顯示EL表達式的原因)
提高web.xml最上面dtd的版本
在jsp頁面添加<%@ page isELIgnored=“false” %> ,添加head里就行

名稱空間
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
關于服務網(wǎng)關Spring Cloud Zuul(Finchley版本)
這篇文章主要介紹了關于服務網(wǎng)關Spring Cloud Zuul(Finchley版本),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)
折半查找法也叫做?分查找,顧名思義就是把數(shù)據(jù)分成兩半,再判斷所查找的key在哪?半中,再重復上述步驟知道找到?標key,下面這篇文章主要介紹了圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)2022-09-09
關于Object中equals方法和hashCode方法判斷的分析
今天小編就為大家分享一篇關于關于Object中equals方法和hashCode方法判斷的分析,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
Spring Cloud Gateway全局通用異常處理的實現(xiàn)
這篇文章主要介紹了Spring Cloud Gateway全局通用異常處理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
JavaWeb項目web.xml中出現(xiàn)Element xxx is not al
這篇文章主要介紹了JavaWeb項目web.xml中出現(xiàn)Element xxx is not allowed here問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Java設計模式之單態(tài)模式(Singleton模式)介紹
這篇文章主要介紹了Java設計模式之單態(tài)模式(Singleton模式)介紹,本文講解了如何使用單例模式、使用單例模式注意事項等內(nèi)容,需要的朋友可以參考下2015-03-03
Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常
這篇文章主要介紹了Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常,@ControllerAdvice ,@ControllerAdvice是一個非常有用的注解,顧名思義,這是一個增強的 Controller,一般配合@ExceptionHandler使用來處理全局異常,需要的朋友可以參考下2024-01-01

