spring aop保存日志案例,附有项目下载链接

jdk:jdk1.8 

工具:eclipse

自己新建个web工程,

本文参考:https://blog.csdn.net/qq_37980469/article/details/80649633


第一步,自定义注解类:

package com.vsked.test.userservice;

import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
   
@Retention(RetentionPolicy.RUNTIME)  
@Target({ElementType.METHOD})  
public @interface BussAnnotation {  
     //模块名  
     String moduleName();  
     //操作内容  
     String option();  
 } 

第二步,切面类:

package com.vsked.test.userservice;

import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Pointcut;  
import org.springframework.stereotype.Component;  
   
   
 @Aspect  
 @Component  
 public class LogInterceptor {  
   
     @Pointcut("execution(public * com.vsked..*.addUser(..))")  
     public void aApplogic() {}  
   
     @Around(value = "aApplogic() && @annotation(annotation) &&args(object,..) ", argNames = "annotation,object")  //@Around括号里面其实可以直接写@Around(“aApplogic()”),这可以拦截所有@Pointcut切点表达式下的所有方法。
     public Object interceptorApplogic(ProceedingJoinPoint pj,BussAnnotation annotation, Object object) throws Throwable {  
    	 System.out.println("ssssssssssssssss");
         System.out.println("LogInterceptor_moduleName:"+annotation.moduleName());  
         System.out.println("LogInterceptor_option:"+annotation.option());  
           
         return pj.proceed();//这里要返回pj.proceed(),否则需要加切面的方法无法return
     }  
 } 

j简单介绍下@execution 里面的public 表示要被切面的方法是要public修饰的,比如是private修饰的就不行。

第三步:service层,需要记录日志的地方

接口类:

package com.vsked.test.userservice;

public interface UserManagerApplogic {
	public void addUser(String name);  
}

实现类:

package com.vsked.test.userservice;

   
 import org.springframework.stereotype.Component;  
   
 @Component("userManager")   
 public class UserManagerApplogicImpl implements UserManagerApplogic {  
   
     @BussAnnotation(moduleName="人员管理",option="添加用户")  
     public void addUser(String name) {  
         System.out.println("UserManagerApplogicImpl_add a User!Name is "+name);  
     }  
 }  


applicationContext.xml 文件配置容器需要扫描的包,如果在测试没有运行成功,很有可能是配置这里没有放在正确的配置文件中,项目比较大的有多个功能类似applicationContext.xml的文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
     <context:annotation-config />  
     <context:component-scan base-package="com.vsked"/>  
     <aop:aspectj-autoproxy />  
</beans>  

最后是测试类:

package com.vsked.test.userservice;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestLogInterceptor {

	@Test
	public void testLogInterceptor() {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
		UserManagerApplogic userManager = (UserManagerApplogic) ctx	.getBean("userManager");
		userManager.addUser("mynameisvsked");
		((AbstractApplicationContext) ctx).destroy();
	}
	
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
		UserManagerApplogic userManager = (UserManagerApplogic) ctx.getBean("userManager");
		userManager.addUser("mynameisvsked");
		 ((AbstractApplicationContext) ctx).destroy();
	}

}

运行即可!

目录结构如下:

这里是代码的链接地址,是个web工程,可下载下来直接运行:

链接:https://pan.baidu.com/s/1EN-Q7Tv-ZzgetuoJNY2avw 
提取码:988t 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老马识途2.0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值