拦截接口上的注解

2021-10-09  本文已影响0人  夜月河色

主要用于拦截service层,监听PO变化,实现PO到DTO的更新。
1.配置类

@Configuration
public class AnnotationAopConfig {

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        return new DefaultAdvisorAutoProxyCreator();// 这个类就是自动代理创建器,能够自动的为每个bean生成代理
    }
}

2.aop父类

public abstract class AnnotationAop<A extends Annotation> extends StaticMethodMatcherPointcutAdvisor implements MethodInterceptor {

    public AnnotationAop(){
        this.setAdvice(this);
        this.setOrder(1);//设置顺序,值越小,优先级越高,也就是越被先执行
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        ReflectiveMethodInvocation in= (ReflectiveMethodInvocation) invocation;
        doBefore(in);
        Object object = invocation.proceed();
        doAfter(in,object);
        return object;
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        return method.isAnnotationPresent(pointCut()) || targetClass.isAnnotationPresent(pointCut());
    }

    //获取执行对象
    protected Object getTarget(ReflectiveMethodInvocation invocation){
        return invocation.getThis();
    }

    //获取方法
    protected Method getMethod(ReflectiveMethodInvocation invocation){
        return invocation.getMethod();
    }

    //获取参数
    protected Object[] getArgs(ReflectiveMethodInvocation invocation){
        return invocation.getArguments();
    }

    //获取注解
    protected A getAnnotation(ReflectiveMethodInvocation invocation){
        return invocation.getMethod().getAnnotation(pointCut());
    }

    abstract public Class<A> pointCut();

    abstract public void doBefore(ReflectiveMethodInvocation invocation);

    abstract public void doAfter(ReflectiveMethodInvocation invocation,Object result);

}

3.自定义注解

@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ListenDBOperate {
    DBOperate value();
}

4.aop类

@Component
public class ListenDBOperateAop extends AnnotationAop<ListenDBOperate> {
    @Override
    public Class<ListenDBOperate> pointCut() {
        return ListenDBOperate.class;
    }

    @Override
    public void doBefore(ReflectiveMethodInvocation invocation) {
        System.out.printf("1:before:%s:%s:%s:%s%n"
                , getTarget(invocation).getClass().getName()
                , getMethod(invocation).getName()
                , JSON.toJSONString(getArgs(invocation))
                , getAnnotation(invocation).value());
    }

    @Override
    public void doAfter(ReflectiveMethodInvocation invocation, Object result) {
        System.out.printf("1:after:%s:%s:%s:%s%n"
                , getTarget(invocation).getClass().getName()
                , getMethod(invocation).getName()
                , JSON.toJSONString(getArgs(invocation))
                , getAnnotation(invocation).value());
    }

}

5.监听接口

public interface EntitySerFunction<T,I> {
    //创建
    @ListenDBOperate(DBOperate.CREATE)
    T create(T entity);

    //删除
    @ListenDBOperate(DBOperate.DELETE)
    int delete(I id);

    //修改
    @ListenDBOperate(DBOperate.UPDATE)
    T update(T entity);

    //刷新
    @ListenDBOperate(DBOperate.FLUSH)
    T flush(T entity);
}

6.测试类

class CarTest extends BaseJunit {
    @Autowired
    private UserSer ser;

    @Test
    void case1(){
        User user=new User();
        user.setName("赵妹妹");
        user.setAge(33);
        user=ser.create(user);

        ser.select(user.getId());

        user.setName("赵姐姐");
        user.setAge(34);
        user=ser.update(user);

        user.setName("赵老婆");
        user=ser.flush(user);

        ser.delete(user.getId());

        User user1=new User();
        user1.setName("测试");
        user1.setAge(18);
        ser.create(user1);
    }
}

7.结果

1:before:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:create:[{"age":33,"name":"赵妹妹"}]:CREATE
1:after:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:create:[{"age":33,"id":70,"name":"赵妹妹"}]:CREATE
1:before:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:update:[{"age":34,"id":70,"name":"赵姐姐"}]:UPDATE
1:after:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:update:[{"age":34,"id":70,"name":"赵姐姐"}]:UPDATE
1:before:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:flush:[{"age":34,"id":70,"name":"赵老婆"}]:FLUSH
1:after:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:flush:[{"age":34,"id":70,"name":"赵老婆"}]:FLUSH
1:before:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:delete:[70]:DELETE
1:after:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:delete:[70]:DELETE
1:before:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:create:[{"age":18,"name":"测试"}]:CREATE
1:after:com.o3gene.listen_jpa.entity.user.UserSerImp$$EnhancerBySpringCGLIB$$63f1ae0:create:[{"age":18,"id":71,"name":"测试"}]:CREATE
上一篇下一篇

猜你喜欢

热点阅读