Springboot uses aop facets to automatically switch data sources, so why can't the order of facets be prior to the order of transactions?

transactions using aspectj proxy
Agent order order=2

@EnableDubboConfiguration
@EnableAspectJAutoProxy
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableTransactionManagement(order = 2, mode = AdviceMode.ASPECTJ)

corresponding pom configuration:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <showWeaveInfo>true</showWeaveInfo>
                    <forceAjcCompile>true</forceAjcCompile>
                    <sources/>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                    </weaveDirectories>
                    <source>1.8</source>
                    <target>1.8</target>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>1.8.9</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>1.8.9</version>
                    </dependency>
                </dependencies>
            </plugin>

defined aop section
order=1

@Aspect
@Order(1)
@Component
public class BocDynamicDSAspect {

    @Before("@annotation(bocDS)")
    public void beforeSwitchDS(JoinPoint point, BocDS bocDS) {
        //class
        Class<?> className = point.getTarget().getClass();

        //
        String methodName = point.getSignature().getName();
        //
        Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
        String dataSource = BocDSHolder.DEFAULT_DS;
        try {
            Method method = className.getMethod(methodName, argClass);

            if (method.isAnnotationPresent(BocDS.class)) {
                BocDS annotation = method.getAnnotation(BocDS.class);
                // 
                dataSource = annotation.value();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 
        BocDSHolder.switchDB(dataSource);
    }

implementation of switching data sources:

public class BocDynamicDS extends AbstractRoutingDataSource {

    public static final Logger logger = LoggerFactory.getLogger(BocDynamicDS.class);

    @Override
    protected Object determineCurrentLookupKey() {
        return BocDSHolder.getDBKey();
    }

}

the expected assumption is:
first execute the aop section to obtain the corresponding database information
and then the transaction initialization starts to obtain the data source information defined in the comments obtained in the aop section
now the situation is:
transactions are always initialized before the aop aspect, and then the aop aspect for data source switching is executed, and the whole data source switching fails

.

how do the transactions and aspects of the aspectj agent ensure order? Find the solution

Mar.04,2021

encounter the same problem, how is it solved?

Menu