思考以下方式在记录日志方式中的使用.(将日志当成一个切面)
日志的类型信息,详细信息等,定义在注解中.
使用注解的时候,填写上.
在aop中,记录日志的时候,将这些信息写到日志中
- 编写拦截规则的注解
package com.hgh.springboot_1.test1_3_2;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 注解本身是没有功能的,就和xml一样.
* 注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置.
* @author Administrator
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
- 使用注解的被拦截类
package com.hgh.springboot_1.test1_3_2;
import org.springframework.stereotype.Service;
/**
* 使用注解的被拦截类
* @author 13076
*
*/
@Service
public class DemoAnnotationService {
@Action(name="注解式拦截的add操作")
public void add(){
System.out.println("执行了DemoAnnotationService的add方法");
}
}
- 使用方法规则的被拦截类
package com.hgh.springboot_1.test1_3_2;
import org.springframework.stereotype.Service;
/**
* 使用方法规则的被拦截类
* @author 13076
*
*/
@Service
public class DemoMethodServices {
public void add(){
}
}
- 切面
package com.hgh.springboot_1.test1_3_2;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
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.stereotype.Component;
@Aspect //声明一个切面
@Component
public class LogAspect {
//声明一个切点
@Pointcut("@annotation(com.hgh.springboot_1.test1_3_2.Action)")
public void annotationPoinCut(){
}
/**
* 基于注解的拦截
* @param joinPoint
*/
//声明一个建言
@After("annotationPoinCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
//通过反射可以获得注解上的属性,然后做日志记录相关的操作,下面的相同.
System.out.println("注解式拦截: " + action.name());
}
/**
* 基于方法的拦截
* @param joinPoint
*/
@Before("execution(* com.hgh.springboot_1.test1_3_2.DemoMethodServices.*(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则拦截: " + method.getName());
}
}
- 配置类
package com.hgh.springboot_1.test1_3_2;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configurable
@ComponentScan("com.hgh.springboot_1.test1_3_2")
//开启spring对aspectj的支持
@EnableAspectJAutoProxy
public class AopConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodServices demoMethodServices = context.getBean(DemoMethodServices.class);
demoAnnotationService.add();
demoMethodServices.add();
context.close();
}
}
输出
执行了DemoAnnotationService的add方法
注解式拦截: 注解式拦截的add操作
方法规则拦截: add
Pom.xml
这里写代码片