Spring AOP 实现业务日志记录

40 篇文章 0 订阅

1. 用户管理业务逻辑接口(UserManagerApplogic.java )

Java代码   收藏代码
  1. package com.iteye.applogic;  
  2.   
  3. public interface UserManagerApplogic {  
  4.     public void addUser(String name);  
  5. }  

 

2. 用户管理业务逻辑实现类(UserManagerApplogicImpl.java)

Java代码   收藏代码
  1. package com.iteye.applogic.impl;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. import com.iteye.applogic.UserManagerApplogic;  
  6. import com.iteye.annotation.BussAnnotation;  
  7. @Component("userManager")   
  8. public class UserManagerApplogicImpl implements UserManagerApplogic {  
  9.   
  10.     @BussAnnotation(moduleName="人员管理",option="添加用户")  
  11.     public void addUser(String name) {  
  12.         System.out.println("add a User!Name is "+name);  
  13.     }  
  14. }  
 

3.业务注释类(BusAnnotation.java)

Java代码   收藏代码
  1. package com.iteye.annotation;  
  2.    
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Target({ElementType.METHOD})  
  10. public @interface BussAnnotation {  
  11.     //模块名  
  12.     String moduleName();  
  13.     //操作内容  
  14.     String option();  
  15. }  

 

(1)RetentionPolicy(保留策略)是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME。

SOURCE 代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。 

ClASS的 代表的是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS。

RUNTIME代表的是表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的。

 

(2)ElementType

@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.

TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上. 

FIELD(属性)

METHOD(方法)

PARAMETER(参数)

CONSTRUCTOR(构造函数)

LOCAL_VARIABLE(局部变量)

ANNOTATION_TYPE

PACKAGE(包)

 

(3)@Documented

@Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息。

 

(4)@Inherited

如果需要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型。

 

 4.切面类(LogInterceptor.java)

Java代码   收藏代码
  1. package com.iteye.aop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.Around;  
  5. import org.aspectj.lang.annotation.Aspect;  
  6. import org.aspectj.lang.annotation.Pointcut;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. import com.iteye.annotation.BussAnnotation;  
  10.   
  11. @Aspect  
  12. @Component  
  13. public class LogInterceptor {  
  14.   
  15.     @Pointcut("execution(public * com.iteye..*.addUser(..))")  
  16.     public void aApplogic() {}  
  17.   
  18.     @Around(value = "aApplogic() && @annotation(annotation) &&args(object,..) ", argNames = "annotation,object")  
  19.     public Object interceptorApplogic(ProceedingJoinPoint pj,  
  20.             BussAnnotation annotation, Object object) throws Throwable {  
  21.         System.out.println("moduleName:"+annotation.moduleName());  
  22.         System.out.println("option:"+annotation.option());  
  23.         pj.proceed();  
  24.         return object;  
  25.     }  
  26. }  

 

5.配置文件(applicationContext-aop.xml)

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop  
  11.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  12.     <context:annotation-config />  
  13.     <context:component-scan base-package="com.iteye"/>  
  14.     <aop:aspectj-autoproxy />  
  15. </beans>  

 

6.测试类( test.java (Junit) )

Java代码   收藏代码
  1. import org.junit.Test;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3. import com.iteye.applogic.UserManagerApplogic;  
  4.   
  5. public class test {  
  6.     @Test  
  7.     public void test1()  
  8.     {  
  9.         ClassPathXmlApplicationContext ctx =   
  10.                 new ClassPathXmlApplicationContext("applicationContext-aop.xml");  
  11.         UserManagerApplogic userManager = (UserManagerApplogic) ctx.getBean("userManager");  
  12.         userManager.addUser("-li.bb-");  
  13.         ctx.destroy();  
  14.     }  
  15. }  
 

 

Spring AOP是一个强大的框架,可以帮助我们实现各种切面,其中包括日志记录。下面是实现日志记录的步骤: 1. 添加Spring AOP依赖 在Maven或Gradle中添加Spring AOP依赖。 2. 创建日志切面 创建一个用于记录日志的切面。这个切面可以拦截所有需要记录日志的方法。在这个切面中,我们需要使用@Aspect注解来声明这是一个切面,并使用@Pointcut注解来定义哪些方法需要被拦截。 ```java @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.demo.service.*.*(..))") public void serviceMethods() {} @Around("serviceMethods()") public Object logServiceMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 获取方法名,参数列表等信息 String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); // 记录日志 System.out.println("Method " + methodName + " is called with args " + Arrays.toString(args)); // 执行方法 Object result = joinPoint.proceed(); // 记录返回值 System.out.println("Method " + methodName + " returns " + result); return result; } } ``` 在上面的代码中,我们使用了@Around注解来定义一个环绕通知,它会在拦截的方法执行前后执行。在方法执行前,我们记录了该方法的名称和参数列表,然后在方法执行后记录了该方法的返回值。 3. 配置AOPSpring的配置文件中配置AOP。首先,我们需要启用AOP: ```xml <aop:aspectj-autoproxy/> ``` 然后,我们需要将创建的日志切面添加到AOP中: ```xml <bean id="loggingAspect" class="com.example.demo.aspect.LoggingAspect"/> <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.demo.service.*.*(..))"/> <aop:around method="logServiceMethods" pointcut-ref="serviceMethods"/> </aop:aspect> </aop:config> ``` 在上面的代码中,我们将创建的日志切面声明为一个bean,并将其添加到AOP中。我们还定义了一个切入点,并将其与日志切面的方法进行关联。 4. 测试 现在,我们可以测试我们的日志记录功能了。在我们的业务逻辑中,所有匹配切入点的方法都会被拦截,并记录它们的输入和输出。我们可以在控制台中看到这些日志信息
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值