Problem of multi-source debugging + Aop expression

Keywords: Spring Java

Method Annotation and Class Annotation

Key Points for Integration of AbstractRouting Data Source from Multiple Data Sources

  • Annotation: For classes and methods (acting on the service layer)
  • Aop Interceptor: Intercept operational expressions by intercepting all methods under the service class:
@Pointcut("execution(* com.ma.service.*.impl..*.*(..))")
  • Use of method annotations: @Pointcut(value ="@annotation (com.ma.annotation.DynamicSource))
  • The use of class annotations: @Pointcut("execution(* com.ma.service..impl... *. ))”) Scanning under a package
  • Note: Classes with annotations for switching data sources and one more thing is that all methods are implemented to effectively dynamically switch data sources otherwise annotations are invalid
@Service
@DynamicSource("temp")
public class SubCustomersPrizeServiceImpl extends BaseCoreServiceImpl<SubCustomersPrizesMapper, SubCustomersPrizes> implements SubCustomersPrizeService {

    @Override
    public int count(BaseExample example) {
        return super.count(example);
    }
}    
  • The reason for class annotations: After testing, without writing implementation methods, annotation listeners go to parent class implementations, switching data sources first and then setting threadlocal values. The correct process is to set threadlocal values before switching data sources.
  • What needs to be improved: Annotation classes need to be able to achieve content values without writing code [very important]
  • Where testing is done, try not to use @Transactional where switching data sources are used [with this annotation affecting the order value of the transaction, which is set to 1 by default]
  • Think about whether it's the annotations that cause errors in testing multiple data sources.
@ On the within class and its method @annotation only work on the method

If the parent class has annotations, but the child class has no annotations, @within and @target will not take effect on the child class.
In the absence of annotations for subclasses, only annotated parent methods that are not overridden can be matched by @within.
If the parent class has no annotations and the child class has annotations, @target takes effect on all methods of the parent class and @within takes effect only on overloaded methods.

Expression

    /**
     * Execution (method expression)
     * 1,When matching methods, only the implementation class can be matched, and the interface class can not be matched successfully.
     * 2,Matching method execution
     */
    // Match any method execution under cn.timebusker.service package and subpackage
    @Pointcut(value = "execution(* cn.timebusker.service.*.*(..))")
    public void log1() {
    }

    // Match any service package under any package and any method execution under subpackages (this pattern can only match to one-level subpackages, and multilevel subpackages are not applicable)
    @Pointcut(value = "execution(* *..service.*.*(..))")
    public void log2() {
    }

    // Match any service package under any package and any method execution under a subpackage (this pattern can match any method execution under any multilevel subpackage)
    @Pointcut(value = "execution(* *..service..*(..))")
    public void log3() {
    }

    // Matching the execution of service packages and methods under any package with a return value type of java.lang.String
    @Pointcut(value = "execution(java.lang.String *..service..*(..))")
    public void log4() {
    }

    // Method execution under service package and subpackage matching any package with return value type int
    @Pointcut(value = "execution(int *..service..*(..))")
    public void log5() {
    }

    // Method execution matching cn.timebusker package of any return value type and Strign type of parameter starting with add under any subpackage
    @Pointcut(value = "execution(* cn.timebusker..add*(String))")
    public void log6() {
    }

    // Matching OR, AND
    @Pointcut(value = "execution(* cn.timebusker.service.*.add*(int))")
    public void log7() {
    }

    // Matching OR, AND,
    @Pointcut(value = "execution(* cn.timebusker.service.*.add*(int)) OR execution(* cn.timebusker..add*(String))")
    public void log8() {
    }

    /**
     * Within (type expression)
     * 1,When matching type, only the implementation class can be matched, but the interface class can not be matched successfully.
     * 2,Matches method execution within a specified type;
     */
    // Matching method execution within a specified type -- only matching type
    @Pointcut(value = "within(cn.timebusker.service.order.Impl.OrderInfoServiceImpl)")
    public void logw1() {
    }

    // Matches method execution within a specified type (all classes under the package)
    @Pointcut(value = "within(cn.timebusker.service.order.Impl.*)")
    public void logw2() {
    }

    /**
     * 3. This (type fully qualified name)
     * 1,Full-name qualified type matching can be accomplished by directly matching interface types
     * 2,Note that AOP proxy object type matching, which may include the introduction of interface methods can also be matched; note that the expression used in this must be a fully qualified type name, which does not support wildcards
     */
    // Matches method execution within a specified type (all classes under the package)
    @Pointcut(value = "this(cn.timebusker.service.order.OrderInfoService)")
    public void logt1() {
    }

    /**
     * 4. Target (Fully qualified name of type) -- Execution method matching current target object type
     * 1,Full-name qualified type matching can be accomplished by directly matching interface types
     * 2,Note that the type matching of the target object does not include introducing interfaces and type matching; note that the expression used in the target must be a fully qualified type name and does not support wildcards
     */
    // Matches method execution within a specified type (all classes under the package)
    @Pointcut(value = "target(cn.timebusker.service.order.OrderInfoService)")
    public void logt2() {
    }

/**
 * 5. args (list of parameter types) -- Execution methods that match the parameters passed in by the currently executed method to the specified type
 * 1,Note that the input parameter type is matched, not the parameter type of the matching method signature; the parameter in the parameter type list must be the fully qualified name of the type, which is not supported by wildcards;
 * 2,args It belongs to dynamic entry point and matches dynamically at runtime. This entry point is very expensive and should not be used in non-special cases.
 * 3,No examples are given here.
 */

    /**
     * 6. @within (annotation type) - matches so holds the method within the specified annotation type; the annotation type must also be a fully qualified type name;
     * 1,Annotation types must also be fully qualified type names.
     */
    // Matching the class annotated by the annotation org. spring framework. stereotype. Service - - annotation does not work on the interface
    @Pointcut(value = "@within(org.springframework.stereotype.Service)")
    public void logaw1() {
    }

    // Classes that match custom annotations -- annotations don't work on interfaces
    @Pointcut(value = "@within(cn.timebusker.annotation.timebuskerBean)")
    public void logaw2() {
    }

/**
 * 7. @target (annotation type) -- Execution method matching current target object type
 * 1,The target object holds the specified annotations;
 * 2,Annotation types must also be fully qualified type names.
 * 3,No examples are given here.
 */

    /**
     * 8. @annotation (annotation type) -- Matches the current method of execution with the specified annotation
     * 1,Annotation types must also be fully qualified type names.
     */
    // Classes that match custom annotations -- annotations don't work on interface methods
    @Pointcut(value = "@annotation(cn.timebusker.annotation.timebuskerMethod)")
    public void logaa1() {
    }

Posted by SulkyGirl on Mon, 29 Jul 2019 02:43:46 -0700