java基础之-自定义注解三 综合使用实例

需求:对增加注解的方法入参进行校验。
目标:使用简单 可扩展性强 支持按顺序处理

  1. 改造之前的ShowMessage注解
@Retention(RetentionPolicy.RUNTIME)
public @interface ShowMessage {
    /**
     * value是注解的成员变量(当只有一个成员变量时必须用value) 可以使用 default指定默认值。
     * 成员变量的类型限定必须是:基本的数据类型以及String,Class,Annotation,Enumeration
     * @return
     */
    String key() default "";

    String value() default "";
}
  1. 定义接口IAnnotation用于扩展
package com.annotation.Comprehensive;
import java.util.Map;
public interface IAnnotation {
    public String getType();
    public void process(Object[] argsa,Map param);
}

3.定义工厂类获取接口实现

package com.annotation.Comprehensive;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class AnnotationFacty implements ApplicationContextAware {
    public static Map<String, IAnnotation> beans;
    //接口全部实现
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        beans = new LinkedHashMap<>();
        Map<String, IAnnotation> beanMap = applicationContext.getBeansOfType(IAnnotation.class);
        for (Map.Entry<String, IAnnotation> entry : beanMap.entrySet()) {
            beans.put(entry.getValue().getType(), entry.getValue());
        }
    }

    public static Map<String, IAnnotation> getBeans() {
        return beans;
    }
}

4.改造之前的ShowMessageAspect切面类

@Aspect
@Component
public class ShowMessageAspect {

    @Pointcut("@annotation(showMessage)")
    public void serviceStatistics(ShowMessage showMessage) {
    }

    /**
     * @param showMessage
     * @Before 前置通知表示再方法执行之前执行
     * 这个aspect 的概念这里不做过多描述。 @annotation是 aspect 表达式。
     */
    @Before("serviceStatistics(showMessage)")
    public void before(JoinPoint joinPoint, ShowMessage showMessage) {
        //1.获取方法添加注解方法的入参
        Object[] argsa = joinPoint.getArgs();
        //2.获取注解入参转换
        Map<String, String> param = new LinkedHashMap();
        param.put(showMessage.key(), showMessage.value());
        //3.调用自定义处理
        Map<String, IAnnotation> beans = AnnotationFacty.getBeans();
        if (beans == null || beans.isEmpty()) {
            return;
        }
        //调用接口方法处理
        for (Map.Entry<String, String> entry : param.entrySet()) {
            if (beans.containsKey(entry.getKey())) {
                beans.get(entry.getKey()).process(argsa, param);
            }
        }
    }
}

这样呢整个框架部分就完成了,下来看看怎么使用。
1.定义一个IAnnotation实现。实现getType方法(注意 getType返回的字符串 与使用注解的入参 key 必须一致否则找不到该实现)来看看代码吧。

package com.annotation.Comprehensive;
import org.springframework.stereotype.Component;
import java.util.Map;
//实现类
@Component
public class Test implements IAnnotation {
    @Override
    public String getType() {
        return "TEST";
    }
    @Override
    public void process(Object[] argsa, Map param) {
        System.out.print(argsa.toString());
    }
}

调用部分

//使用注解
@Repository
public class AnnotationUtile {
    private static Logger logger= LoggerFactory.getLogger(AnnotationUtile.class);
    //使用上面自定义的注解
   //这里value 可为多个值用“,”隔开 自行再实现类中处理
    @ShowMessage(key = "TEST",value = "aaaa")
    public void test(String value,int a,Object object){
        logger.debug("测试信息");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43545983/article/details/84073212