一、靈活配置DataSource

一、本課目標(biāo)

  • 能夠使用屬性文件配置數(shù)據(jù)源
  • 能夠使用JNDI數(shù)據(jù)源

二、配置數(shù)據(jù)源

2.1使用屬性文件配置數(shù)據(jù)源

  • 數(shù)據(jù)庫信息寫在屬性文件中
  • 采用PropertyPlaceholderConfigurer可以引入屬性文件,在Spring配置文件中采用注入${url}的方式引用屬性值。


    image.png

    image.png

    配置文件:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
    <!-- 配置數(shù)據(jù)源 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:database.properties</value>
        </property>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations">
            <list>
                <value>classpath:cn/smbms/dao/**/*.xml</value>
            </list>
        </property>
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.smbms.dao"></property>
    </bean>
    <!-- Service -->
    <context:component-scan base-package="cn.smbms.service.user"></context:component-scan>
    <!-- 事務(wù)管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <tx:annotation-driven transaction-manager="txManager"/>
    
    <!-- 事務(wù)增強 -->
    <!-- <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" timeout="1000"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice> -->
    <!-- 配置切面 -->
    <!-- <aop:config>
        <aop:pointcut expression="execution(* cn.smbms.service..*.*(..))" id="myPoint"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint"/>
    </aop:config> -->
</beans>

dataSource.properties文件:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=41312019

2.2使用JNDI數(shù)據(jù)源

dbcp的數(shù)據(jù)源相當(dāng)于是針對于當(dāng)前這個項目的,只能為當(dāng)前項目所使用,其他項目不能共享;而jndi數(shù)據(jù)源是存儲在服務(wù)器之中的,服務(wù)器中可能會有很多項目,而這很多項目都可以去共享這個數(shù)據(jù)源。
通過JNDI從服務(wù)器容器中獲取DataSource資源

  • 在服務(wù)器環(huán)境中配置JNDI數(shù)據(jù)源
  • 在Spring配置文件引用JNDI資源
image.png

配置JNDI數(shù)據(jù)源步驟:
1、在tomcat服務(wù)器文件中:
conf——context.xml下面增加一段代碼:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,272評論 6 342
  • application的配置屬性。 這些屬性是否生效取決于對應(yīng)的組件是否聲明為Spring應(yīng)用程序上下文里的Bea...
    新簽名閱讀 5,539評論 1 27
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 要加“m”說明是MB,否則就是KB了. -Xms:初始值 -Xmx:最大值 -Xmn:最小值 java -Xms1...
    阿B和阿C閱讀 7,527評論 0 7
  • 不是生活在溫室里的花,不會經(jīng)歷不起一點風(fēng)雨,不會什么都需要父母操心,很多事情可以自己解決,可以獨立生活。如果說有什...
    秋燃講故事閱讀 143評論 0 0

友情鏈接更多精彩內(nèi)容