這篇文章主要講解了“Spring多數(shù)據(jù)源AOP動(dòng)態(tài)切換怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Spring多數(shù)據(jù)源AOP動(dòng)態(tài)切換怎么實(shí)現(xiàn)”吧!
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、欽北ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的欽北網(wǎng)站制作公司一:新增多數(shù)據(jù)源類(lèi)
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
點(diǎn)擊(此處)折疊或打開(kāi)
public class DataSourceContextHolder {
private static final ThreadLocal
public static void setDataSource(String dataSource) {
contextHolder.set(dataSource);
}
public static String getDataSource() {
return contextHolder.get();
}
}
二:新增注解
點(diǎn)擊(此處)折疊或打開(kāi)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface DataSource {
String value();
}
三:新增AOP切面
點(diǎn)擊(此處)折疊或打開(kāi)
@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();
}
}
}
四:數(shù)據(jù)源配置
點(diǎn)擊(此處)折疊或打開(kāi)
@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;
}
}
五:應(yīng)用舉例
點(diǎn)擊(此處)折疊或打開(kāi)
@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多數(shù)據(jù)源AOP動(dòng)態(tài)切換怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Spring多數(shù)據(jù)源AOP動(dòng)態(tài)切換怎么實(shí)現(xiàn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!