使Spring Boot 自定义注解支持EL表达式

自定义注解

  • 自定义DistributeExceptionHandler注解,该注解接收一个参数attachmentId
  • 该注解用在方法上,使用该注解作为切点,实现标注该注解的方法抛异常后的统一处理。
/**
 * @Author: Lijian
 * @Date: 2019-09-06 09:26
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributeExceptionHandler {
    String attachmentId();
}

  • 使用方法:
@DistributeExceptionHandler(attachmentId = "#test.id")
public void test(Test test){
    
}

Aspect代码

  • 拦截DistributeExceptionHandler注解作为切点
  • 使用@AfterThrowing处理异常情况
/**
 * @Author: Lijian
 * @Date: 2019-09-06 09:22
 */
@Component
@Aspect
@Slf4j
public class DistributeExceptionAspect {

    @Autowired
    private AttachmentContentClient attachmentContentClient;

    @Autowired
    private DistTaskService distTaskService;

    private ExpressionEvaluator<String> evaluator = new ExpressionEvaluator<>();

    @Pointcut("@annotation(DistributeExceptionHandler)")
    private void exceptionHandleMethod() {

    }

    @AfterThrowing(value = "exceptionHandleMethod()", throwing = "ex")
    public void doThrowing(JoinPoint joinPoint, Throwable ex) {
        log.error("捕获异常");
        String attachmentId = getAttachmentId(joinPoint); // 获取
        // 处理异常情况下的业务
    }

    private DistributeExceptionHandler getDistributeExceptionHandler(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        return method.getAnnotation(DistributeExceptionHandler.class);
    }

    private String getAttachmentId(JoinPoint joinPoint) {
        DistributeExceptionHandler handler = getDistributeExceptionHandler(joinPoint);
        if (joinPoint.getArgs() == null) {
            return null;
        }
        EvaluationContext evaluationContext = evaluator.createEvaluationContext(joinPoint.getTarget(), joinPoint.getTarget().getClass(), ((MethodSignature) joinPoint.getSignature()).getMethod(), joinPoint.getArgs());
        AnnotatedElementKey methodKey = new AnnotatedElementKey(((MethodSignature) joinPoint.getSignature()).getMethod(), joinPoint.getTarget().getClass());
        return evaluator.condition(handler.attachmentId(), methodKey, evaluationContext, String.class);
    }
}

为注解添加Spring EL支持

  • ExpressionRootObject
/**
 * @Author: Lijian
 * @Date: 2019-09-06 09:26
 */
public class ExpressionRootObject {
    private final Object object;
    private final Object[] args;

    public ExpressionRootObject(Object object, Object[] args) {
        this.object = object;
        this.args = args;
    }

    public Object getObject() {
        return object;
    }

    public Object[] getArgs() {
        return args;
    }
}

  • ExpressionEvaluator

/**
 * @Author: Lijian
 * @Date: 2019-09-06 09:26
 */
public class ExpressionEvaluator<T> extends CachedExpressionEvaluator {
    private final ParameterNameDiscoverer paramNameDiscoverer = new DefaultParameterNameDiscoverer();
    private final Map<ExpressionKey, Expression> conditionCache = new ConcurrentHashMap<>(64);
    private final Map<AnnotatedElementKey, Method> targetMethodCache = new ConcurrentHashMap<>(64);


    public EvaluationContext createEvaluationContext(Object object, Class<?> targetClass, Method method, Object[] args) {
        Method targetMethod = getTargetMethod(targetClass, method);
        ExpressionRootObject root = new ExpressionRootObject(object, args);
        return new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
    }


    public T condition(String conditionExpression, AnnotatedElementKey elementKey, EvaluationContext evalContext, Class<T> clazz) {
        return getExpression(this.conditionCache, elementKey, conditionExpression).getValue(evalContext, clazz);
    }

    private Method getTargetMethod(Class<?> targetClass, Method method) {
        AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
        Method targetMethod = this.targetMethodCache.get(methodKey);
        if (targetMethod == null) {
            targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
            if (targetMethod == null) {
                targetMethod = method;
            }
            this.targetMethodCache.put(methodKey, targetMethod);
        }
        return targetMethod;
    }
}

备注

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Configuration注解是一种用于配置Spring应用程序的注解方式。它提供了一种简化配置的方式,可以通过注解来声明和管理Spring Bean,以及配置和解决Bean之间的依赖关系。 Spring提供了多个@Configuration注解相关的注解,包括@ComponentScan、@EnableAutoConfiguration和@Import等。 @ComponentScan注解用于自动扫描指定的包路径,将标有@Component、@Service、@Repository等注解的类注册为Spring Bean。它还可以通过指定excludeFilters和includeFilters来控制扫描过程中的过滤规则。 @EnableAutoConfiguration注解用于启用Spring Boot自动配置机制,它会根据classpath中的jar包和类路径来自动配置Spring应用程序所需的Bean和环境。它基于@SpringBootApplication注解,并通过@SpringBootApplication注解的派生性质来实现。 @Import注解用于将其他的配置类导入到当前配置类中,从而实现配置类的组合和复用。被导入的配置类可以是普通的@Configuration注解标注的类,也可以是带有@Bean注解的方法所在的类。 除了上述几个主要的注解外,Spring Configuration注解还包括了@Bean注解用于声明一个方法可以生成一个Spring Bean,@PropertySource注解用于加载外部的配置文件,@Value注解用于注入配置文件中的属性值等。 总的来说,Spring Configuration注解提供了一种简洁而灵活的方式来配置和管理Spring应用程序,可以通过注解的方式来声明和注册Spring Bean,实现依赖注入,同时还可以通过其他注解来加载配置文件和注入属性值,进一步提高应用程序的可维护性和扩展性。 ### 回答2: Spring注解配置是指使用注解来配置Spring应用程序的一种方式。在过去,Spring配置主要使用XML文件进行,但自从Spring 2.5版本推出以来,通过注解配置的方式逐渐成为主流。 使用注解配置可以简化开发人员的工作,使配置更加集中和易于管理。在使用注解配置时,只需在需要配置的类或方法上添加特定的注解,就能实现相应的配置。 常见的Spring注解配置包括以下几类: 1. @Configuration: 它是一个类级别的注解,用于标记一个类作为配置类。配置类中的方法通过@Bean注解来声明一个 bean 对象。 2. @ComponentScan: 它是一个类级别的注解,用于自动扫描并注册Spring bean。在注解中可以指定要扫描的包路径或类路径,Spring会自动扫描该路径下的所有类,并将其注册为Spring bean。 3. @Autowired: 它是一个字段级别的注解,用于自动装配Spring bean。当一个类中需要依赖其他类的实例时,使用该注解可以让Spring自动将依赖对象注入到类中。 4. @Value: 它是一个字段级别的注解,用于注入外部配置的值。通过该注解可以将配置文件中的值注入到类的字段中。 通过使用Spring注解配置,可以更加方便地管理和维护Spring应用程序。它减少了繁琐的XML配置,使开发人员能够更专注于业务逻辑的实现。同时,注解配置也使代码更加简洁和易于阅读。但需要注意的是,过度使用注解可能会导致代码可读性下降,所以在使用注解时要慎重选择合适的场景。 ### 回答3: Spring Configuration注解是一种用于配置和管理Spring应用程序的方式。它可以帮助我们简化配置,提高开发效率。 Spring Configuration注解主要包括以下几种: 1. @Configuration:将类标记为配置类,用于替代传统的XML配置文件。在配置类中,我们可以使用其他注解来定义Bean和配置项。 2. @ComponentScan:启用组件扫描,可以自动扫描并注册带有@Component注解的类作为Bean。可以通过指定扫描路径来限制扫描的范围。 3. @Bean:用于定义Bean,在被@Configuration注解标记的类中的方法上使用。方法返回的对象将作为Bean注册到Spring容器中,可以通过依赖注入的方式使用。 4. @Autowired:用于进行依赖注入,将被注解的字段、构造方法或者Setter方法的参数与容器中的Bean进行关联。它可以根据类型进行自动匹配或者通过@Qualifier注解根据名称进行匹配。 5. @Value:用于将配置项注入到Bean中,可以用来注入配置文件中的值,也可以用来注入通过Spring EL表达式计算的值。 除了以上的注解Spring Configuration还提供了一些其他注解,如@Profile用于定义不同环境下的配置,@Import用于导入其他配置类,@PropertySource用于指定配置文件等。 通过使用Spring Configuration注解,我们可以摆脱繁琐的XML配置文件,更加简洁地配置和管理Spring应用程序。它使得代码结构更加清晰,便于维护和扩展。同时,通过注解方式配置的应用程序更加便于测试和集成,提高了整体开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值