您好,登錄后才能下訂單哦!
這篇文章主要講解了“Spring多數據源AOP動態切換怎么實現”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring多數據源AOP動態切換怎么實現”吧!
一:新增多數據源類
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
點擊(此處)折疊或打開
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setDataSource(String dataSource) {
contextHolder.set(dataSource);
}
public static String getDataSource() {
return contextHolder.get();
}
}
二:新增注解
點擊(此處)折疊或打開
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface DataSource {
String value();
}
三:新增AOP切面
點擊(此處)折疊或打開
@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();
}
}
}
四:數據源配置
點擊(此處)折疊或打開
@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<Object, Object> 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;
}
}
五:應用舉例
點擊(此處)折疊或打開
@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);
}
}
}
感謝各位的閱讀,以上就是“Spring多數據源AOP動態切換怎么實現”的內容了,經過本文的學習后,相信大家對Spring多數據源AOP動態切換怎么實現這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。