spring aop保存日志案例,附有项目下载链接[通俗易懂]

spring aop保存日志案例,附有项目下载链接[通俗易懂]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();
	}

}

运行即可!

目录结构如下:

spring aop保存日志案例,附有项目下载链接[通俗易懂]

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/105994.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号