Spring多數(shù)據(jù)源AOP動態(tài)切換怎么實現(xiàn)-創(chuàng)新互聯(lián)

這篇文章主要講解了“Spring多數(shù)據(jù)源AOP動態(tài)切換怎么實現(xiàn)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Spring多數(shù)據(jù)源AOP動態(tài)切換怎么實現(xiàn)”吧!

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、欽北ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的欽北網(wǎng)站制作公司

一:新增多數(shù)據(jù)源類

  1. public class DynamicDataSource extends AbstractRoutingDataSource {

  2.     @Override

  3.     protected Object determineCurrentLookupKey() {

  4.         return DataSourceContextHolder.getDataSource();

  5.     }

  6. }

點擊(此處)折疊或打開

  1. public class DataSourceContextHolder {

  2.     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

  3.     public static void setDataSource(String dataSource) {

  4.         contextHolder.set(dataSource);

  5.     }

  6.     public static String getDataSource() {

  7.         return contextHolder.get();

  8.     }

  9. }


二:新增注解

點擊(此處)折疊或打開

  1. @Retention(RetentionPolicy.RUNTIME)

  2. @Target(ElementType.METHOD)

  3. @Documented

  4. public @interface DataSource {

  5.     String value();

  6. }


三:新增AOP切面

點擊(此處)折疊或打開

  1. @Aspect

  2. @Component

  3. public class DataSourceAspect {

  4.     @Pointcut("@annotation(com.gemdale.ghome.business.async.deal.center.demo.datasource.DataSource)")

  5.     public void dataSourcePointCut() {

  6.     };

  7.     @Before("dataSourcePointCut()")

  8.     public void before(JoinPoint joinPoint) {

  9.         System.out.println("=============dataSourcePointCut:before=============");

  10.         Object target = joinPoint.getTarget();

  11.         String method = joinPoint.getSignature().getName();

  12.         // Class<?>[] classz = target.getClass().getInterfaces();

  13.         Class<?> classz = target.getClass();

  14.         Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();

  15.         try {

  16.             // Method m = classz[0].getMethod(method, parameterTypes);

  17.             Method m = classz.getMethod(method, parameterTypes);

  18.             if (null != m && m.isAnnotationPresent(DataSource.class)) {

  19.                 DataSource dataSource = m.getAnnotation(DataSource.class);

  20.                 DataSourceContextHolder.setDataSource(dataSource.value());

  21.                 System.out.println("=============dataSource:" + dataSource.value());

  22.             }

  23.         }

  24.         catch (Exception e) {

  25.             e.printStackTrace();

  26.         }

  27.     }

  28. }


四:數(shù)據(jù)源配置

點擊(此處)折疊或打開

  1. @Configuration

  2. public class DynamicTransactionManagerElConfig {

  3.     @Autowired

  4.     @Qualifier("platformTomcat")

  5.     private DataSource platformTomcat;

  6.     @Autowired

  7.     @Qualifier("platformReadTomcat")

  8.     private DataSource platformReadTomcat;

  9.     @Bean(name = "dataSource")

  10.     public DynamicDataSource dataSource() {

  11.         DynamicDataSource dataSource = new DynamicDataSource();

  12.         Map<Object, Object> targetDataSources = new HashMap<>();

  13.         targetDataSources.put("master", platformTomcat);

  14.         targetDataSources.put("slave", platformReadTomcat);

  15.         dataSource.setTargetDataSources(targetDataSources);

  16.         dataSource.setDefaultTargetDataSource(platformTomcat);

  17.         return dataSource;

  18.     }

  19.     

  20.     

  21.     @Bean(name = "jdbcTemplate")

  22.     public JdbcTemplate jdbcTemplate(DynamicDataSource dataSource) {

  23.         JdbcTemplate jdbcTemplate = new JdbcTemplate();

  24.         jdbcTemplate.setDataSource(dataSource);

  25.         return jdbcTemplate;

  26.     }

  27.     @Bean(name = "jdbcReadTemplate")

  28.     public JdbcTemplate jdbcReadTemplate(DynamicDataSource dataSource) {

  29.         JdbcTemplate jdbcReadTemplate = new JdbcTemplate();

  30.         jdbcReadTemplate.setDataSource(dataSource);

  31.         return jdbcReadTemplate;

  32.     }

  33.     

  34.     @Bean(name = "transactionManager")

  35.     public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) {

  36.         DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();

  37.         transactionManager.setDataSource(dataSource);

  38.         return transactionManager;

  39.     }

  40. }


五:應(yīng)用舉例

點擊(此處)折疊或打開

  1. @Service("gmcSmsInfoBo")

  2. public class GmcSmsInfoBo extends AbstractBusinessObject {

  3.     @Autowired

  4.     private GmcSmsInfoDAO gmcSmsInfoDaoImpl;

  5.     // @CachePut(value = "GmcSmsInfoCache", key = "'GmcSmsInfo_'+#result.smsId")

  6.     // @Transactional(rollbackFor={Exception.class,RuntimeException.class})

  7.     @DataSource("master")

  8.     public GmcSmsInfo add(GmcSmsInfo smsInfo) throws BusinessServiceException {

  9.         System.out.println("=============add==========");

  10.         try {

  11.             smsInfo.setSmsId(gmcSmsInfoDaoImpl.save(smsInfo));

  12.         }

  13.         catch (FrameworkDAOException e) {

  14.             throw new BusinessServiceException(e);

  15.         }

  16.         return smsInfo;

  17.     }

  18.     // @Cacheable(value="GmcSmsInfoCache",key="'GmcSmsInfo_'+#smsId")

  19.     @DataSource("slave")

  20.     public GmcSmsInfo query(Integer smsId) throws BusinessServiceException {

  21.         System.out.println("=============query==========");

  22.         try {

  23.             return gmcSmsInfoDaoImpl.findById(GmcSmsInfo.class, smsId);

  24.         }

  25.         catch (Exception e) {

  26.             throw new BusinessServiceException(e);

  27.         }

  28.     }

  29. }

感謝各位的閱讀,以上就是“Spring多數(shù)據(jù)源AOP動態(tài)切換怎么實現(xiàn)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Spring多數(shù)據(jù)源AOP動態(tài)切換怎么實現(xiàn)這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

網(wǎng)站名稱:Spring多數(shù)據(jù)源AOP動態(tài)切換怎么實現(xiàn)-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://bm7419.com/article30/dicopo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)頁設(shè)計公司電子商務(wù)、云服務(wù)器、移動網(wǎng)站建設(shè)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)