Spring加載屬性文件方式(自動(dòng)加載優(yōu)先級(jí)問(wèn)題)
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ù)庫(kù)的鏈接信息寫到屬性文件中,如下。
jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.driver=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=123456
在spring配置文件中,就可以用${}的形式獲取屬性信息,但需要加入 <context:property-placeholder />標(biāo)簽設(shè)置屬性文件的路徑。即
?<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>
但是由此會(huì)引發(fā)另一個(gè)問(wèn)題,自動(dòng)加載的優(yōu)先級(jí)特別高(就是先實(shí)例化)
若org.mybatis.spring.SqlSessionFactoryBean的id為sqlSessionFactory,當(dāng)自動(dòng)注入時(shí),org.mybatis.spring.mapper.MapperScannerConfigurer類下的SqlSessionFactory屬性會(huì)自動(dòng)注入,然后org.mybatis.spring.SqlSessionFactoryBean也會(huì)實(shí)例化,而org.mybatis.spring.SqlSessionFactoryBean中含有dateSourse,所以org.springframework.jdbc.datasource.DriverManagerDataSource也會(huì)實(shí)例化,但是這時(shí)屬性文件還沒(méi)有加載,造成程序出錯(cuò)Error setting property values,總而言之就是在屬性文件加載之前,類實(shí)例化了,結(jié)果得不到屬性文件中的值。
解決辦法
第1步,更改org.mybatis.spring.SqlSessionFactoryBean的id名稱,例如factory
第2步,將org.mybatis.spring.mapper.MapperScannerConfigurer中加入<property name="sqlSessionFactoryBeanName" value="factory"></property>,如果用<property name="sqlSessionFactory/>標(biāo)簽同樣出現(xiàn)以上的問(wèn)題。
因?yàn)樽詣?dòng)注入只影響ref的,而sqlSessionFactoryBeanName的值的類型時(shí)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ù)庫(kù)連接,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對(duì)象 --> ? ? ? <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> ? ? ? ?? ?<!-- 數(shù)據(jù)庫(kù)連接信息來(lái)源于dataSource --> ? ? ? ?? ?<!-- <property name="dataSource" ref="dataSource"></property> --> ? ? ? ?? ?<!-- 相當(dāng)于mybatis中別名默認(rèn)包 --> ? ? ? ?? ?<property name="typeAliasesPackage" value="com.lee.pojo"></property> ? ? ? </bean> ? ? ? <!-- 掃描器相當(dāng)于mybatis設(shè)置接口綁定時(shí)xml的mappers下的package標(biāo)簽 --> ? ? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> ? ? ? ?? ?<!-- 掃描哪個(gè)包 --> ? ? ? ?? ?<property name="basePackage" value="com.lee.mapper"></property> ? ? ? ?? ?<!-- 和factory產(chǎn)生關(guān)系 -->?? ? ?? ? ? ? <property name="sqlSessionFactoryBeanName" value="factory"></property> ? ? ? </bean> ? ?? </beans>
方式2、用注解
使用注解方法時(shí),需要添加標(biāo)簽,這里的包名指的是含有注解的類所在包
<context:component-scan base-package="com.lee.service.impl"></context:component-scan>
測(cè)試的properties
my.value=hello
測(cè)試類
public class Demo{ ?? ?@Value("${my.value}") ?? ?private String test;?? ? }
這樣就可以實(shí)例化Demo時(shí)給test注入值
對(duì)Spring加載順序的理解
web.xml初始化
- Web項(xiàng)目啟動(dòng)的時(shí)候,容器(如:tomcat)讀取webapp/WEB-INF/web.xml文件,讀取和;
- 創(chuàng)建ServletContex,Web項(xiàng)目所有部分都可以使用該上下文ServletContex;
- 容器將解析為key-value對(duì),并交給ServletContext;
- 容器根據(jù)中的類創(chuàng)建監(jiān)聽(tīng)實(shí)例,即啟動(dòng)監(jiān)聽(tīng);
- listener監(jiān)聽(tīng)類中會(huì)contextInitialized(ServletContextEvent servletContextEvent)初始化方法,可通過(guò)ServletContextEvent.getServletContext().getInitParameter(“field”)獲得value的值;
- 解析,并啟動(dòng)攔截器 攔截器開(kāi)始起作用,當(dāng)有請(qǐng)求進(jìn)入時(shí),執(zhí)行Filter的doFilter方法;
- 最后加載和初始化配置在load on startup的servlets;
- 加載Spring,如果filter需要用到bean,但加載順序是: 先加載filter 后加載spring,則filter中初始化操作中的bean為null.如果過(guò)濾器中要使用到 bean,可以將spring 的加載 改成Listener的方式:org.springframework.web.context.ContextLoaderListener
先創(chuàng)建上下文對(duì)象servletcontext,再加載監(jiān)聽(tīng)器,然后去加載攔截器,最后加載servlet
路徑問(wèn)題:Spring MVC靜態(tài)資源攔截(No mapping found for HTTP request with URI in DispatcherServlet with name ’ ')問(wèn)題
/ 是加載視圖配置的目錄下的文件,前提是webapp下沒(méi)有默認(rèn)文件;如果有文件就訪問(wèn)默認(rèn)文件
/* 我的測(cè)試是都報(bào)404
spring加載流程
啟動(dòng)先加載web.xml(包含:加載applicationContext.xml、listener:contextloadlistener、:DispatcherServlet),通過(guò)applicationContext.xml加載接口及java實(shí)現(xiàn)類、加載config.properties文件、加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)等、加載mybatis.config文件(SqlSessionFactoryBean:加載xml文件)、加載數(shù)據(jù)庫(kù)的接口和mapper.xml、加載springmvc視圖等。
要保證install后mapper.java、mapper.xml要在同一文件下
如果用EL表達(dá)式(ModelAndView)時(shí)表達(dá)式出現(xiàn)問(wèn)題解決如下:(搜索:SpringMVC中JSP頁(yè)面不顯示EL表達(dá)式的原因)
提高web.xml最上面dtd的版本
在jsp頁(yè)面添加<%@ page isELIgnored=“false” %> ,添加head里就行
名稱空間
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播
這篇文章主要為大家詳細(xì)介紹了java使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Java中的強(qiáng)制類型轉(zhuǎn)換 大數(shù)轉(zhuǎn)小數(shù)
這里主要討論一下大數(shù)轉(zhuǎn)小數(shù),比如int類型轉(zhuǎn)short類型。小數(shù)轉(zhuǎn)大數(shù),如short 轉(zhuǎn) int不做討論,需要的朋友可以參考下2020-02-02關(guān)于@RequestParam注解的使用(簡(jiǎn)單易懂)
這篇文章主要介紹了關(guān)于@RequestParam注解的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java switch case數(shù)據(jù)類型原理解析
這篇文章主要介紹了Java switch case數(shù)據(jù)類型原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01springcloud引入spring-cloud-starter-openfeign失敗的解決
這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03plantuml畫(huà)圖實(shí)現(xiàn)代碼畫(huà)時(shí)序圖UML用例圖
這篇文章主要為大家介紹了plantuml畫(huà)圖實(shí)現(xiàn)代碼畫(huà)時(shí)序圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07