最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring加載屬性文件方式(自動加載優(yōu)先級問題)

 更新時間:2022年02月14日 11:12:08   作者:后知后覺后海  
這篇文章主要介紹了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 />標(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>

但是由此會引發(fā)另一個問題,自動加載的優(yōu)先級特別高(就是先實例化)

若org.mybatis.spring.SqlSessionFactoryBean的id為sqlSessionFactory,當(dāng)自動注入時,org.mybatis.spring.mapper.MapperScannerConfigurer類下的SqlSessionFactory屬性會自動注入,然后org.mybatis.spring.SqlSessionFactoryBean也會實例化,而org.mybatis.spring.SqlSessionFactoryBean中含有dateSourse,所以org.springframework.jdbc.datasource.DriverManagerDataSource也會實例化,但是這時屬性文件還沒有加載,造成程序出錯Error setting property values,總而言之就是在屬性文件加載之前,類實例化了,結(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)以上的問題。

因為自動注入只影響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> -->
? ? ? ?? ?<!-- 相當(dāng)于mybatis中別名默認包 -->
? ? ? ?? ?<property name="typeAliasesPackage" value="com.lee.pojo"></property>
? ? ? </bean>
? ? ? <!-- 掃描器相當(dāng)于mybatis設(shè)置接口綁定時xml的mappers下的package標(biāo)簽 -->
? ? ? <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ?? ?<!-- 掃描哪個包 -->
? ? ? ?? ?<property name="basePackage" value="com.lee.mapper"></property>
? ? ? ?? ?<!-- 和factory產(chǎn)生關(guān)系 -->?? ? ??
? ? ? <property name="sqlSessionFactoryBeanName" value="factory"></property>
? ? ? </bean> ? ??
</beans>

方式2、用注解

使用注解方法時,需要添加標(biāo)簽,這里的包名指的是含有注解的類所在包

<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的值;
  • 解析,并啟動攔截器 攔截器開始起作用,當(dāng)有請求進入時,執(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)

    關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)

    這篇文章主要介紹了關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳解Struts2動態(tài)方法調(diào)用

    詳解Struts2動態(tài)方法調(diào)用

    這篇文章主要介紹了詳解Struts2動態(tài)方法調(diào)用,涉及調(diào)用方法的代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-09-09
  • 深入淺出的學(xué)習(xí)Java ThreadLocal

    深入淺出的學(xué)習(xí)Java ThreadLocal

    本文會基于實際場景介紹ThreadLocal如何使用以及內(nèi)部實現(xiàn)機制。 具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • 圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)

    圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)

    折半查找法也叫做?分查找,顧名思義就是把數(shù)據(jù)分成兩半,再判斷所查找的key在哪?半中,再重復(fù)上述步驟知道找到?標(biāo)key,下面這篇文章主要介紹了圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)
    2022-09-09
  • 關(guān)于Object中equals方法和hashCode方法判斷的分析

    關(guān)于Object中equals方法和hashCode方法判斷的分析

    今天小編就為大家分享一篇關(guān)于關(guān)于Object中equals方法和hashCode方法判斷的分析,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Spring Cloud Gateway全局通用異常處理的實現(xiàn)

    Spring Cloud Gateway全局通用異常處理的實現(xiàn)

    這篇文章主要介紹了Spring Cloud Gateway全局通用異常處理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • JavaWeb項目web.xml中出現(xiàn)Element xxx is not allowed here問題

    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設(shè)計模式之單態(tài)模式(Singleton模式)介紹

    Java設(shè)計模式之單態(tài)模式(Singleton模式)介紹

    這篇文章主要介紹了Java設(shè)計模式之單態(tài)模式(Singleton模式)介紹,本文講解了如何使用單例模式、使用單例模式注意事項等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • SpringBoot進行參數(shù)校驗的方法詳解

    SpringBoot進行參數(shù)校驗的方法詳解

    在日常的接口開發(fā)中,為了防止非法參數(shù)對業(yè)務(wù)造成影響,經(jīng)常需要對接口的參數(shù)進行校驗。本文通過示例詳細講解了SpringBoot如何進行參數(shù)校驗的,感興趣的可以學(xué)習(xí)一下
    2022-04-04
  • Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常

    Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常

    這篇文章主要介紹了Spring中的@ControllerAdvice和@ExceptionHandler注解處理全局異常,@ControllerAdvice ,@ControllerAdvice是一個非常有用的注解,顧名思義,這是一個增強的 Controller,一般配合@ExceptionHandler使用來處理全局異常,需要的朋友可以參考下
    2024-01-01

最新評論

密云县| 临武县| 册亨县| 剑河县| 宜丰县| 安宁市| 武清区| 新密市| 南澳县| 兴义市| 奉贤区| 武隆县| 枣阳市| 通榆县| 手游| 东兰县| 昌乐县| 同江市| 名山县| 南溪县| 天镇县| 东乡族自治县| 濮阳市| 兴安县| 库尔勒市| 浦北县| 南京市| 黔西| 海兴县| 凤翔县| 绥滨县| 四平市| 南陵县| 化州市| 鄄城县| 东丰县| 大足县| 阿勒泰市| 扶余县| 甘德县| 八宿县|