修改别人写的利用AOP实现日志监控的问题

时间:2022-04-23 22:20:16

原文链接

http://blog.csdn.net/jaune161/article/details/51476138

想法

看到原文博主写的这篇文章,我感觉写的很好,可以在我们的项目中使用AOP来监控用户的操作,后台管理员根据AOP记录的内容来实现统计功能,进行绩效考核

流程

Created with Raphaël 2.1.0开始编写User实体类编写业务逻辑接口类编写业务逻辑实现类编写AOP业务逻辑类编写而junit测试类

AOP应用参考文章

  1. http://www.cnblogs.com/wayfarer/articles/241024.html
  2. http://blog.csdn.net/moreevan/article/details/11977115/
  3. http://www.infoq.com/cn/articles/Orchestration-Oleg-Zhurakousky/

下面的内容主要是修复原博主文章中的不足的地方


User类

package com.springinaction.springidol;
public class User { private String username;
private String password; public User() {
super();
} public User(String username, String password) {
super();
this.username = username;
this.password = password;
} public String getUsername() {
return username;
} public String getPassword() {
return password;
}
}

UserService 接口类

package com.springinaction.springidol;
//接口
public interface UserService {
public void login(User user) throws AuthenticationException;
}

UserServiceImp (实现类)

package com.springinaction.springidol;
//实现类
public class UserServiceImpl implements UserService { public void login(User user) throws AuthenticationException {
if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
System.out.println("登录成功");
}else{
throw new AuthenticationException("用户名密码错误!");
}
} }

织入点声明

这个类是实现登陆监控的主要业务逻辑实现.

1. 该类声明了一个切点loginPointcut, 使用aspect的语法来声明一个织入点

2. Around等注解的使用,请参考其他文章

package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class LoginMonitor { @Pointcut("execution(* com.springinaction.springidol.UserService.login(..))")
public void loginPointcut(){}; @Around("loginPointcut()")
public Object afterLogin(ProceedingJoinPoint point) throws Throwable{
User user = (User) point.getArgs()[0];
System.out.println(user.getUsername()+"请求登录系统");
Object obj ;
try {
obj = point.proceed();
} catch (Throwable e) {
System.out.println(user.getUsername()+"登录失败!"+e.getMessage());
throw e;
}
System.out.println(user.getUsername()+"已登录系统");
return obj;
} }

Spring的配置文件

<?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.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config></context:annotation-config>
<bean id="userService" class="com.springinaction.springidol.UserServiceImpl"/>
<bean class="com.springinaction.springidol.LoginMonitor"/>
<aop:aspectj-autoproxy/> <!-- 支持@Aspect注解 -->
</beans>

Junit测试类

package com.springinaction.springidol;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* Created by shugen on 16-6-2.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol.xml")
public class TestCLass {
@Autowired
private UserService userService; @Test
public void testLogin() throws AuthenticationException {
User user = new User("admin","admin");
userService.login(user);
}
}

POM.xml(这里使用maven进行项目管理)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>rmb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm-commons</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm-util</artifactId>
<version>3.2</version>
</dependency> </dependencies>
<packaging>war</packaging>
<build>
<finalName>Vue</finalName>
</build>
</project>

注意问题

如果使用了1.6.2 的aspectjweaver和aspectjrt可能会导致一下错误

Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut loginPointcut

详细错误信息

Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut loginPointcut
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:193)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:182)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:163)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:209)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:263)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:295)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply

解决办法,使用高版本的aspectjweaver和aspectjrt

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>

程序运行验证

修改别人写的利用AOP实现日志监控的问题