springboot 基于注解路由方法

package com.example.demo13.annotation;

import com.example.demo13.service.AnnoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/anno")
public class AnnoController {

    @Autowired
    private AnnoService annoService;

    /**
     * 测试
     * @param requestInt
     * @return
     */
    @GetMapping("/hello")
    public String hello(int requestInt){

        return annoService.hello(requestInt);
    }
}
package com.example.demo13.annotation;

import org.springframework.stereotype.Service;

/**
 * 基于注解来执行默认的还是切面插入的方法
 */
@Service
public class AnnotationImpl implements AnnotationInterface {

    @Override
    public Object execute(String annotation, Object[] args){
        return "通过注解返回";
    }
}
package com.example.demo13.annotation;

/**
 * 基于注解来执行默认的还是切面插入的方法
 */
public interface AnnotationInterface {

    public Object execute(String annotation, Object[] args);
}
package com.example.demo13.annotation;

import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 模拟日志
 */
@Order(98)// 控制多个Aspect的执行顺序,越小越先执行
@Aspect
@Component
public class AspectStyle {
    @Pointcut("execution(* com.example..*.*(..))")
    public void init() {
    }

    @Before(value = "init()")
    public void before() {
        System.out.println("方法执行前执行.....");
    }

    @AfterReturning(value = "init()")
    public void afterReturning() {
        System.out.println("方法执行完执行.....");
    }

    @AfterThrowing(value = "init()")
    public void throwss() {
        System.out.println("方法异常时执行.....");
    }

    @After(value = "init()")
    public void after() {
        System.out.println("方法最后执行.....");
    }

    /*@Around(value = "init()")
    public Object around(ProceedingJoinPoint pjp) {
        System.out.println("方法环绕start.....");
        Object o = null;
        try {
            o = pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("方法环绕end.....");
        return o;
    }*/
}

package com.example.demo13.annotation;

import java.lang.annotation.*;

/**
 * 注解
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyLog {
    String value() default "hello world";
}

package com.example.demo13.annotation;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 基于注解来配置走老方法还是新切入的方法
 */
@Order(99) //控制多个Aspect的执行顺序,越小越先执行
@Aspect
@Component
@Slf4j
public class OperateAspect {

    @Autowired
    private AnnotationInterface annoService;

    @Pointcut("@annotation(com.example.demo13.annotation.MyLog)")
    public void annotationPointCut() {
    }

    @Before("annotationPointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println(123);
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        MyLog annotation = method.getAnnotation(MyLog.class);
        String value = annotation.value();
        System.out.println(value);
    }

    @Around("annotationPointCut()  && @annotation(annotation)")
    public Object around(ProceedingJoinPoint pjp, MyLog annotation) throws Throwable         {
        System.out.println("方法环绕start.....");

        /* 或者这种方式获取annotation MethodSignature signature = (MethodSignature)pjp.getSignature();
        Method method = signature.getMethod();
        MyLog annotation = method.getAnnotation(MyLog.class);*/
        String value = annotation.value();
        Object o ;
        if (StringUtils.equalsIgnoreCase("hello", value)){
            o = pjp.proceed();
        } else {
            Object[] args = pjp.getArgs();
            o = annoService.execute(value, args);
        }
        System.out.println("方法环绕end.....");
        return o;
    }
}

package com.example.demo13.service;

import com.example.demo13.annotation.MyLog;
import org.springframework.stereotype.Service;

@Service
public class AnnoService {

    @MyLog("hello world")
    public String hello(int value){
        return "hello world" + value;
    }

    public String good(int value){
        return "good world" + value;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值