利用springAOP+@Conditional注解拦截定时任务

由来

正常在公司的项目中, 为了保证项目的可靠性, 同一个项目可能会部署到多台服务器上, 通常点说的话就是集群.
那么, 在部署集群项目的时候. 定时任务的运行就是一个问题了. 我们只需要一台机器运行定时任务. 因此, 就需要把其他机器上的定时任务屏蔽掉. 那么, 就需要一个拦截定时任务的功能.

创建一个定时任务

启动类添加@EnableScheduling注解,开启定时任务功能

创建一个每五秒执行一次的定时任务


import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author: luobendexiaoqiang
 * @Date: 2020/7/24 19:39
 */
@Component
@Slf4j
public class TaskTest {
    /**
     * 五秒执行一次
     */
    @Scheduled(fixedRate = 5 * 1000)
    public void taskTest() {
        log.info("五秒一次定时任务开始执行!!");
    }

}

启动项目, 定时任务启动成功, 每五秒执行一次

在这里插入图片描述

添加springaop依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

创建定时任务拦截类


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Author: luobendexiaoqiang
 * @Date: 2020/7/24 19:48
 */
@Slf4j
@Component()
@Aspect
public class FilterScheduledAop {


    @Value("${scheduled.filter:#{false}}")
    private Boolean scheduledFilter;


    /* 拦截@Scheduled注解 */
    @Pointcut(" @annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void proxyAspect() {

    }

    /**
     * 拦截不到private方法
     *
     * @param joinPoint
     * @return
     */
    @Around("proxyAspect()")
    public Object doInvoke(ProceedingJoinPoint joinPoint) {
        if (!scheduledFilter) {
            try {
                log.info("开始执行定时任务!!!");
                //执行被拦截的方法
                return joinPoint.proceed();
            } catch (Throwable e) {
                log.error("定时器拦截器异常", e);
            }
        } else {
            log.info("定时任务已被拦截!!!");
        }

        return null;
    }
}

application.properties配置文件添加scheduled.filter:true, true表示要拦截定时器,默认不拦截

在这里插入图片描述
定时任务已经被拦截, 定时任务方法不再被执行

从以上情况也能看出, 利用了AOP拦截住定时器之后, 不管需不需要运行定时任务, 定时任务都会被拦截. 在这种情况下, 当我们需要执行定时任务的时候, 每个定时任务都会被AOP无端的拦截一次. 那么, 有什么方法能让我们在需要执行定时任务的时候, 不被拦截呢?

使用@Conditional注解

创建TaskFilterCondition类并实现Condition接口

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @Author: qinqiang
 * @Date: 2020/7/24 15:22
 */
public class TaskFilterCondition implements Condition {


    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取配置文件中scheduled.filter的值
        String filterschedule = conditionContext.getEnvironment().getProperty("scheduled.filter");
        return "true".equals(filterschedule);
    }
}
修改AOP拦截类

import com.springboot.service.condition.TaskFilterCondition;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

/**
 * @Author: luobendexiaoqiang
 * @Date: 2020/7/24 19:48
 */
@Slf4j
@Component()
@Aspect
@Conditional(TaskFilterCondition.class)
public class FilterScheduledAop {


    /* 拦截@Scheduled注解 */
    @Pointcut(" @annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void proxyAspect() {

    }

    /**
     * 拦截不到private方法
     *
     * @param joinPoint
     * @return
     */
    @Around("proxyAspect()")
    public Object doInvoke(ProceedingJoinPoint joinPoint) {

        log.info("定时任务已被拦截!!!");

        return null;
    }
}

@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。

也就是说, 只有TaskFilterCondition类中的matches方法返回true时, AOP拦截类才会被实例化

  • 当我们不需要关闭定时任务的时候, 我们不实例化AOP拦截类, 定时任务当然就不会被无端的拦截了.
  • 当我们需要关闭定时任务的时候, 我们再实例化AOP拦截类, 来实现拦截定时任务的操作
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先,我们需要定义一个注解 @RequiresPermissions,用于标记需要权限控制的方法。代码如下: ``` @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RequiresPermissions { String[] value(); } ``` 然后,我们需要定义一个切面类,用于拦截被 @RequiresPermissions 标记的方法,并进行权限控制的逻辑。代码如下: ``` @Component @Aspect public class PermissionAspect { @Autowired private PermissionService permissionService; @Pointcut("@annotation(com.example.demo.annotation.RequiresPermissions)") public void requiresPermissionsPointcut() {} @Around("requiresPermissionsPointcut()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { // 获取当前用户信息 User currentUser = UserContext.getCurrentUser(); // 获取方法上的权限标记 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); RequiresPermissions annotation = signature.getMethod().getAnnotation(RequiresPermissions.class); String[] permissions = annotation.value(); // 校验权限 boolean hasPermission = permissionService.checkPermissions(currentUser, permissions); if (!hasPermission) { throw new UnauthorizedException("没有访问权限"); } // 执行原方法 return joinPoint.proceed(); } } ``` 在上述代码中,我们通过 @Pointcut 注解定义了一个切点,用于匹配被 @RequiresPermissions 标记的方法。然后,在 @Around 注解的方法中,我们获取了当前用户信息,以及方法上的权限标记。接着,我们调用 PermissionService 的 checkPermissions 方法,校验当前用户是否拥有对应的权限。如果权限校验失败,我们抛出 UnauthorizedException 异常,表示没有访问权限。否则,我们执行原方法,并返回执行结果。 最后,我们定义一个 PermissionService 接口,用于查询用户权限信息。具体实现可以根据实际情况进行编写。代码如下: ``` public interface PermissionService { /** * 校验用户是否拥有指定的权限 * * @param user 当前用户 * @param permissions 权限列表 * @return 是否拥有指定权限 */ boolean checkPermissions(User user, String[] permissions); } ``` 综上,我们通过 Spring AOP 和自定义注解,模拟实现了 Shiro 框架的权限控制功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值