Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the dynamic switching of Spring Multi-data Source AOP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize the dynamic switching of Spring multi-data source AOP". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve Spring multi-data source AOP dynamic switching".

One: new data source classes

Public class DynamicDataSource extends AbstractRoutingDataSource {

@ Override

Protected Object determineCurrentLookupKey () {

Return DataSourceContextHolder.getDataSource ()

}

}

Click (here) to collapse or open

Public class DataSourceContextHolder {

Private static final ThreadLocal contextHolder = new ThreadLocal ()

Public static void setDataSource (String dataSource) {

ContextHolder.set (dataSource)

}

Public static String getDataSource () {

Return contextHolder.get ()

}

}

Second, add new notes

Click (here) to collapse or open

@ Retention (RetentionPolicy.RUNTIME)

@ Target (ElementType.METHOD)

@ Documented

Public @ interface DataSource {

String value ()

}

Third: add AOP section

Click (here) to collapse or open

@ Aspect

@ Component

Public class DataSourceAspect {

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

Public void dataSourcePointCut () {

}

@ Before ("dataSourcePointCut ()")

Public void before (JoinPoint joinPoint) {

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

Object target = joinPoint.getTarget ()

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

/ / Class [] classz = target.getClass () .getInterfaces ()

Class classz = target.getClass ()

Class [] parameterTypes = ((MethodSignature) joinPoint.getSignature ()) .getMethod () .getParameterTypes ()

Try {

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

Method m = classz.getMethod (method, parameterTypes)

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

DataSource dataSource = m.getAnnotation (DataSource.class)

DataSourceContextHolder.setDataSource (dataSource.value ())

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

}

}

Catch (Exception e) {

E.printStackTrace ()

}

}

}

Four: data source configuration

Click (here) to collapse or open

@ Configuration

Public class DynamicTransactionManagerElConfig {

@ Autowired

@ Qualifier ("platformTomcat")

Private DataSource platformTomcat

@ Autowired

@ Qualifier ("platformReadTomcat")

Private DataSource platformReadTomcat

@ Bean (name = "dataSource")

Public DynamicDataSource dataSource () {

DynamicDataSource dataSource = new DynamicDataSource ()

Map targetDataSources = new HashMap ()

TargetDataSources.put ("master", platformTomcat)

TargetDataSources.put ("slave", platformReadTomcat)

DataSource.setTargetDataSources (targetDataSources)

DataSource.setDefaultTargetDataSource (platformTomcat)

Return dataSource

}

@ Bean (name = "jdbcTemplate")

Public JdbcTemplate jdbcTemplate (DynamicDataSource dataSource) {

JdbcTemplate jdbcTemplate = new JdbcTemplate ()

JdbcTemplate.setDataSource (dataSource)

Return jdbcTemplate

}

@ Bean (name = "jdbcReadTemplate")

Public JdbcTemplate jdbcReadTemplate (DynamicDataSource dataSource) {

JdbcTemplate jdbcReadTemplate = new JdbcTemplate ()

JdbcReadTemplate.setDataSource (dataSource)

Return jdbcReadTemplate

}

@ Bean (name = "transactionManager")

Public DataSourceTransactionManager transactionManager (DynamicDataSource dataSource) {

DataSourceTransactionManager transactionManager = new DataSourceTransactionManager ()

TransactionManager.setDataSource (dataSource)

Return transactionManager

}

}

Five: application examples

Click (here) to collapse or open

@ Service ("gmcSmsInfoBo")

Public class GmcSmsInfoBo extends AbstractBusinessObject {

@ Autowired

Private GmcSmsInfoDAO gmcSmsInfoDaoImpl

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

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

@ DataSource ("master")

Public GmcSmsInfo add (GmcSmsInfo smsInfo) throws BusinessServiceException {

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

Try {

SmsInfo.setSmsId (gmcSmsInfoDaoImpl.save (smsInfo))

}

Catch (FrameworkDAOException e) {

Throw new BusinessServiceException (e)

}

Return smsInfo

}

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

@ DataSource ("slave")

Public GmcSmsInfo query (Integer smsId) throws BusinessServiceException {

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

Try {

Return gmcSmsInfoDaoImpl.findById (GmcSmsInfo.class, smsId)

}

Catch (Exception e) {

Throw new BusinessServiceException (e)

}

}

}

Thank you for your reading, the above is the content of "how to realize the dynamic switching of Spring multi-data sources AOP". After the study of this article, I believe you have a deeper understanding of how to realize the dynamic switching of Spring multi-data sources AOP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report