版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33212500/article/details/82851216
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;
}
}