zjh
2025-04-11 46f0694ffeaee5168a77d8935784963a3f8ee7aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
    }
}