Java annotation easy to understand series of tutorials three custom annotation demo

According to the previous tutorial, writing custom annotations is divided into three steps: 1. Define the annotation format 2. Configure the meta annotation 3. Define the parameter 4. Define the annotation logic

This article takes writing multiple data source switching annotations as an example to understand the process of writing custom annotations. The logic of multi-data source switching annotations is: this annotation can be used in the method, according to the annotation attribute, switch to the corresponding data before executing the current method source.

1. Define the annotation format

public @interface ChooseDataSource {

 
}

2. Configure meta annotations

Define the location method that the annotation can use, etc., and the life cycle of the annotation.

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ChooseDataSource {

}

3. Define parameters 

Set the parameter to enumeration and configure the default value as the main data source.

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ChooseDataSource {

    AllDatasourceEnum dataSource() default AllDatasourceEnum.MASTER;
}

4. Define annotation logic

(1) Custom annotation: ChooseDataSource 

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ChooseDataSource {

    AllDatasourceEnum dataSource() default AllDatasourceEnum.MASTER;
}

(2) Specific logic usage (aop intercepts before calling annotations) and (reflection to obtain annotations and execution methods)

In order to demonstrate that the code is simpler, the code for multiple data sources is not written here (if you want to know more about it, see another blog to configure springboot multiple data sources: https://blog.csdn.net/Mint6/article/details/92261790 ) In this article, as long as you know what string DynamicDataSourceContextHolder.setDateSoureType("main") sets, you will select the corresponding data source.

Code overall execution logic : intercept the method in the corresponding package through aop, before and after execution, determine whether there is an annotation, if there is, take out the attribute dataSource and set DynamicDataSourceContextHolder.setDateSoureType ("main") to switch the data source .

Establish the effective scope of annotations : where: @Before("execution(public * com.fast.framework.dao.*.*.*(..)) || @annotation(com.fast.framework.annotation.ChooseDataSource)

") is the effective scope of the annotation execution that I set: if the annotation method is used under the com.fast.framework.dao package and the ChooseDataSource annotation method, the annotation execution logic is effective. If this is not the annotation under this package, the use is invalid .

 @Before("execution(public * com.fast.framework.dao.*.*.*(..)) || @annotation(com.fast.framework.annotation.ChooseDataSource)")
    public void before(JoinPoint joinPoint) {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        /*获取方法上的注解*/
        ChooseDataSource chooseDataSource = method.getAnnotation(ChooseDataSource.class);
        //获取注解上的数据源的值的信息
        String dataSourceName = chooseDataSource.dataSource().name();
        if (dataSourceName != null) {
            //给当前的执行SQL的操作设置特殊的数据源的信息
            DynamicDataSourceContextHolder.setDateSoureType(dataSourceName);
        }
    }

(3) Sheet type: AllDatasourceEnum 

public enum AllDatasourceEnum {

     MASTER(0, "主数据库"),
     SECOND(1, "第二个数据库");

     private int code;
     private String msg;

     AllDatasourceEnum(int code, String msg) {
          this.code = code;
          this.msg = msg;
     }

     public int getCode() {
          return code;
     }

     public String getMsg() {
          return msg;
     }
}

Summary : The demo in this chapter should be easy to understand. It mainly depends on the fourth step of defining the annotation execution logic . If you don't understand, look at the articles before and after the series.

Guess you like

Origin blog.csdn.net/Mint6/article/details/103835990