您好,登录后才能下订单哦!
这篇文章主要讲解了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。