package com.ltkj.framework.aspectj; import com.ltkj.db.DataSourceConfig; import com.ltkj.db.DataSourceContextHolder; import com.ltkj.framework.datasource.DynamicDataSourceContextHolder; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Slf4j @Aspect @Component public class TransactionalDataSourceAspect { @Autowired private DataSourceConfig dataSourceConfig; @Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)") public void transactionalMethods() { } @Before("transactionalMethods()") public void switchDataSourceBeforeTransaction() { String hospId = DataSourceContextHolder.getDataSourceKey(); dataSourceConfig.addDataSource(hospId); DataSourceContextHolder.setDataSourceKey(hospId); } }