大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“Spring多数据源AOP动态切换怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring多数据源AOP动态切换怎么实现”吧!
我们提供的服务有:成都做网站、成都网站设计、微信公众号开发、网站优化、网站认证、钦北ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的钦北网站制作公司一:新增多数据源类
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
点击(此处)折叠或打开
public class DataSourceContextHolder {
private static final ThreadLocal
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
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动态切换怎么实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!