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ù)的鏈接信息寫(xiě)到屬性文件中,如下。
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類(lèi)下的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,總而言之就是在屬性文件加載之前,類(lèi)實(shí)例化了,結(jié)果得不到屬性文件中的值。
解決辦法
第1步,更改org.mybatis.spring.SqlSessionFactoryBean的id名稱(chēng),例如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的值的類(lèi)型時(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ù)源封裝類(lèi),數(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)簽,這里的包名指的是含有注解的類(lèi)所在包
<context:component-scan base-package="com.lee.service.impl"></context:component-scan>
測(cè)試的properties
my.value=hello
測(cè)試類(lèi)
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ù)中的類(lèi)創(chuàng)建監(jiān)聽(tīng)實(shí)例,即啟動(dòng)監(jiān)聽(tīng);
- listener監(jiān)聽(tīng)類(lèi)中會(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)類(lèi)、加載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里就行

名稱(chēng)空間
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)
這篇文章主要介紹了關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解Struts2動(dòng)態(tài)方法調(diào)用
這篇文章主要介紹了詳解Struts2動(dòng)態(tài)方法調(diào)用,涉及調(diào)用方法的代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-09-09
深入淺出的學(xué)習(xí)Java ThreadLocal
本文會(huì)基于實(shí)際場(chǎng)景介紹ThreadLocal如何使用以及內(nèi)部實(shí)現(xiàn)機(jī)制。 具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
圖解Java經(jīng)典算法折半查找的原理與實(shí)現(xiàn)
折半查找法也叫做?分查找,顧名思義就是把數(shù)據(jù)分成兩半,再判斷所查找的key在哪?半中,再重復(fù)上述步驟知道找到?標(biāo)key,下面這篇文章主要介紹了圖解Java經(jīng)典算法折半查找的原理與實(shí)現(xiàn)2022-09-09
關(guān)于Object中equals方法和hashCode方法判斷的分析
今天小編就為大家分享一篇關(guān)于關(guān)于Object中equals方法和hashCode方法判斷的分析,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
JavaWeb項(xiàng)目web.xml中出現(xiàn)Element xxx is not al
這篇文章主要介紹了JavaWeb項(xiàng)目web.xml中出現(xiàn)Element xxx is not allowed here問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java設(shè)計(jì)模式之單態(tài)模式(Singleton模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之單態(tài)模式(Singleton模式)介紹,本文講解了如何使用單例模式、使用單例模式注意事項(xiàng)等內(nèi)容,需要的朋友可以參考下2015-03-03
SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解
在日常的接口開(kāi)發(fā)中,為了防止非法參數(shù)對(duì)業(yè)務(wù)造成影響,經(jīng)常需要對(duì)接口的參數(shù)進(jìn)行校驗(yàn)。本文通過(guò)示例詳細(xì)講解了SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的,感興趣的可以學(xué)習(xí)一下2022-04-04
Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常
這篇文章主要介紹了Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常,@ControllerAdvice ,@ControllerAdvice是一個(gè)非常有用的注解,顧名思義,這是一個(gè)增強(qiáng)的 Controller,一般配合@ExceptionHandler使用來(lái)處理全局異常,需要的朋友可以參考下2024-01-01

