spring 更換資料庫

spring 解析之切換資料來源-mysql實現版本

spring 更換資料庫

j

av

a探案

2017-06-08 13:13:26

主要思想:自定義一個註解,此註解標註在方法上,透過反射解析出方法所在的類,遍歷這個類裡的所有方法,找到註解所對應的那個方法,呼叫invoke,執行方法的過程中,用threadLocal儲存的資料來源名字進行動態切換。下面進行詳細講解:

自定義一個註解,作用是標註方法

package com。tiangou。annotation;

import j

av

a。lang。annotation。Documented;

import j

av

a。lang。annotation。ElementType;

import j

av

a。lang。annotation。Retention;

import j

av

a。lang。annotation。RetentionPolicy;

import j

av

a。lang。annotation。Target;

@Documented

@Retention(RetentionPolicy。RUNTIME)

@Target(ElementType。METHOD)

public @inte

rfa

ce DataSource {

String name() default “”;

}

2。ThreadLocal儲存資料庫

package com。tiangou。dataSource;

public class DataSourceContextHolder {

private static final ThreadLocal contextHolder = new ThreadLocal();

public static void setDbType(String dbType) {

3。AbstractRoutingDataSource spring動態切換資料來源

contextHolder。set(dbType);

}

public static String getDbType() {

return ((String) contextHolder。get());

}

public static void clearDbType() {

contextHolder。remove();

}

}

package com。tiangou。dataSource;

import org。springframework。jdbc。datasource。lookup。AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource{

@Override

protected Object determineCurrentLookupKey() {

System。out。println(DataSourceContextHolder。getDbType());

return DataSourceContextHolder。getDbType();

}

}

4。配置檔案中配置aop切面

<?xml version=“1。0” encoding=“UTF-8”?>

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:context=“

http://www。

springframework。org/schema/context”

xmlns:jee=“

http://www。

springframework。org/schema/jee”

xmlns:tx=“

http://www。

springframework。org/schema/tx”

xsi:schemaLocation=“

http://www。

springframework。org/schema/aop

http://www。

springframework。org/schema/aop/spring-aop-4。0。xsd

http://www。

springframework。org/schema/beans

http://www。

springframework。org/schema/beans/spring-beans-4。0。xsd

http://www。

springframework。org/schema/context

http://www。

springframework。org/schema/context/spring-context-4。0。xsd

http://www。

springframework。org/schema/jee

http://www。

springframework。org/schema/jee/spring-jee-4。0。xsd

http://www。

springframework。org/schema/tx

http://www。

springframework。org/schema/tx/spring-tx-4。0。xsd”>

<!—— 配置資料來源 ——>

is

。xml” />

<!—— ——>

<!—— 使用註解的包,包括子集 ——>

class=“com。alibaba。druid。pool。DruidDataSource”>

class=“com。alibaba。druid。pool。DruidDataSource”>

av

a。lang。String”>

<!—— 配置mybat

is

的sqlSessionFactory ——>

is

。spring。SqlSessionFactoryBean”>

<!—— 自動掃描mappers。xml檔案 ——>

<!—— mybat

is

配置檔案 ——>

is

-config。xml”>

<!—— DAO介面所在包名,Spring會自動查詢其下的類 ——>

is

。spring。mapper。MapperScannerConfigurer”>

<!—— 開啟事務註解驅動 ——>

<!—— (事務管理)transaction manager, use JtaTransactionManager for g

lob

al tx ——>

class=“org。springframework。jdbc。datasource。DataSourceTransactionManager”>

<!—— 配置事務通知屬性 ——>

<!—— 定義事務傳播屬性 ——>

av

e*” propagation=“REQUIRED” />

change

*” propagation=“REQUIRED” />

<!—— SpringMVC在超出上傳檔案限制時,會丟擲

org。springframework。web。multipart。MaxUploadSizeExceededException ——>

<!—— 該異常是SpringMVC在檢查上傳的檔案資訊時丟擲來的,而且此時還沒有進入到Controller方法中 ——>

<!—— 遇到

MaxUploadSizeExceededException異常時,自動跳轉到

/WEB-INF/error_fileupload。jsp頁面 ——>

WEB-INF/error_fileupload

<!—— 處理其它異常(包括Controller丟擲的) ——>

av

a。lang。Throwable”>WEB-INF/500

<!—— 配置事務切面 ——>

expression=“execution(* com。tiangou。service。*。*(。。))” />

is

or advice-ref=“txAdvice” pointcut-ref=“serviceOperation” />

<!——

shiroQUANXIAN

——>

<!—— 繼承自AuthorizingRealm的自定義Realm,即指定Shiro驗證使用者登入的類為自定義的ShiroDbRealm。j

av

a ——>

<!—— ——>

<!—— Shiro預設會使用Servlet容器的Session,可透過sessionMode屬性來指定使用Shiro原生Session ——>

<!—— 即,詳細說明見官方文件 ——>

<!—— 這裡主要是設定自定義的單Realm應用,若有多個Realm,可使用‘realms’屬性代替 ——>

<!—— ——>

<!—— ——>

<!—— ——>

<!—— Shiro主過濾器本身功能十分強大,其強大之處就在於它支援任何基於URL路徑表示式的、自定義的過濾器的執行 ——>

<!—— Web應用中,Shiro可控制的Web請求必須經過Shiro主過濾器的攔截,Shiro對基於Spring的Web應用提供了完美的支援 ——>

<!——

rFa

ctoryBean”> ——>

<!—— Shiro的核心安全介面,這個屬性是必須的 ——>

<!—— ——>

<!—— 要求登入時的連結(可根據專案的URL進行替換),非必須的屬性,預設會自動尋找Web工程根目錄下的“/login。jsp”頁面 ——>

<!—— ——>

<!—— 登入成功後要跳轉的連線(本例中此屬性用不到,因為登入成功後的處理邏輯在LoginController裡硬編碼為main。jsp了) ——>

<!—— ——>

<!—— 使用者訪問未對其授權的資源時,所顯示的連線 ——>

<!—— 若想更明顯的測試此屬性可以修改它的值,如unauthor。jsp,然後用[玄玉]登入後訪問/admin/l

is

tUser。jsp就看見瀏覽器會顯示unauthor。jsp ——>

<!—— ——>

<!—— Shiro連線約束配置,即過濾鏈的定義 ——>

<!—— 此處可配合我的這篇文章來理解各個過濾連的作用

http://blog。csdn。net/jadyer/article/details/12172839 ——>

<!—— 下面value值的第一個‘/’代表的路徑是相對於

HttpServletRequest。getContextPath()的值來的 ——>

<!—— anon:它對應的過濾器裡面是空的,什麼都沒做,這裡。do和。jsp後面的*表示引數,比方說login。jsp?main這種 ——>

<!—— authc:該過濾器下的頁面必須驗證後才能訪問,它是Shiro內建的一個攔截器

org。apache。shiro。web。filter。authc。FormAuthenticationFilter ——>

<!—— ——>

<!—— ——>

<!—— /mydemo/login=anon ——>

<!—— /mydemo/getVerifyCodeImage=anon ——>

<!—— /main**=authc ——>

<!—— /user/info**=authc ——>

<!—— /admin/l

is

tUser**=authc,perms[admin:manage] ——>

<!—— ——>

<!—— ——>

<!—— ——>

<!—— 保證實現了Shiro內部lifecycle函式的bean執行 ——>

<!—— ——>

<!—— 開啟Shiro的註解(如@RequiresRoles,@RequiresPerm

is

sions),需藉助SpringAOP掃描使用Shiro註解的類,並在必要時進行安全邏輯驗證 ——>

<!—— 配置以下兩個bean即可實現此功能 ——>

<!—— Enable Shiro Annotations for Spring-configured beans。 Only run after the lifecycleBeanProcessor has run ——>

<!—— 由於本例中並未使用Shiro註解,故註釋掉這兩個bean(個人覺得將許可權透過註解的方式硬編碼在程式中,檢視起來不是很方便,沒必要使用) ——>

<!——

is

orAutoProxyCreator” depends-on=“lifecycleBeanPostProcessor”/>

is

or”>

——>

<!—— 自動掃描 ——>

5。對呼叫的方法上進行辨別,切換資料來源

以上就是動態切換資料來源的講解,需要原始碼的私信我,喜歡小編的給我一波

關注

,謝謝。