1 首先引入jar包
spring.jar aopalliance.jar aspectjrt.jar aspectjtools.jar aspectjweaver.jar cglib-nodep-2.1_3.jar cglib-src-2.2.jar 这些个jar包不一定都用得上,但离配置到现在整理已经比较久了 有点忘了.
2 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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
3 定义 aop拦截
<aop:aspectj-autoproxy proxy-target-class="true" />
<aop:config proxy-target-class="true">
<aop:aspect ref="logMonitor">
<aop:pointcut id="actionPointcut" expression="execution(* com.*.*.*.service.impl.*.create*(..)) || execution(* com.*.*.*.service.impl.*.update*(..)) || execution(* com.*.*.*.service.impl.*.delete*(..))|| execution(* com.*.*.*.service.impl.*.insert*(..))||execution(* com.*.*.service.impl.*.create*(..)) || execution(* com.*.*.service.impl.*.update*(..)) || execution(* com.*.*.service.impl.*.delete*(..))|| execution(* com.*.*.service.impl.*.insert*(..))"/>
<aop:before pointcut-ref="actionPointcut" method="writeLogInfo" />
</aop:aspect>
</aop:config>
<bean id="logMonitor" parent="baseTransactionProxy">
<property name="target">
<bean
class="com.*.common.userlog.monitor.LogMonitor">
<property name="userLogInfoService">
<ref bean="userLogInfoService" />
</property>
</bean>
</property>
</bean>
4 处理拦截的类logMonitor
@Aspect
public class LogMonitor {
/** * 保存变量的ThreadLocal,保持在同一线程中同步数据. */
private static final ThreadLocal SESSION_MAP = new ThreadLocal();
private IUserLogInfoService userLogInfoService;
public IUserLogInfoService getUserLogInfoService() {
return userLogInfoService;
}
public void setUserLogInfoService(IUserLogInfoService userLogInfoService) {
this.userLogInfoService = userLogInfoService;
}
/**
* 获得线程中保存的属性.
*
* @param attribute
* 属性名称
* @return 属性值
*/
public static Object get(String attribute) {
Map map = (Map) SESSION_MAP.get();
return map.get(attribute);
}
/**
* 记录日志信息
* @param joinpoint
* @throws Exception
* @throws IllegalAccessException
*/
public void writeLogInfo(JoinPoint joinpoint) throws Exception, IllegalAccessException {
UserInfoVO sysUse = (UserInfoVO) UserSession.get(Contents.SESSION_USER_KEY);
// 在基类action中 设用户信息
// UserSession.set(Contents.SESSION_USER_KEY, userVO);
// UserSession.set(Contents.SESSION_USER_IP, request.getRemoteAddr());
String userIP = (String) UserSession.get(Contents.SESSION_USER_IP);
String temp = joinpoint.getStaticPart().toShortString();
// 获得操作类
String classType = joinpoint.getTarget().getClass().getName();
// 获得方法名称
String methodName = temp.substring(10, temp.length() - 1);
Class className = Class.forName(classType);
// 获得方法的参数类型数组
Object[] a = joinpoint.getArgs();
Class[] o = new Class[a.length];//
for (int i = 0; i < a.length; i++) {
o[i] = a[i].getClass();
}
Method method = className.getMethod(methodName, o);
LogDesc desc = method.getAnnotation(LogDesc.class);
// 获得方法的操作动作类型说明
String actionDesc = "";
if (desc != null) {
actionDesc = desc.actionDesc();
}
// 记录日志信息
UserLogInfo userLogInfo = new UserLogInfo();
if (sysUse != null) {
userLogInfo.setUserID(sysUse.getUserID());
userLogInfo.setUserIp(userIP);
userLogInfo.setOperateClass(classType);
userLogInfo.setOperateMethod(methodName);
//设置为一般日志信息标识位
userLogInfo.setEnableFlag("C");
userLogInfo.setOperateDescribe(actionDesc);
}
userLogInfoService.addUserLog(userLogInfo);
}
5 定义取得拦截方法描述的类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogDesc {
String actionDesc();
}
6 在每个需要拦截的方法头加上
/**
* 注册新用户
*
* @roseuid 3C30BD930399
*/
@LogDesc(actionDesc = "新建会员")
public MemberVO createMember(MemberVO memberVO) throws SysComException {
return memberRegisterDao.createMember(memberVO);
}
7 设置、取得用户信息
public class UserSession{
// private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();
//
// public HttpServletRequest getContext(){
// return (HttpServletRequest)threadLocal.get();
// }
// public void setContext(HttpServletRequest request){
// threadLocal.set(request);
// }
//
// public void cleanContext(){
// threadLocal.set(null);
// }
/** * 保存变量的ThreadLocal,保持在同一线程中同步数据. */
private static final ThreadLocal SESSION_MAP = new ThreadLocal();
/** * 工具类的protected构造方法. */
protected UserSession() {
}
/**
* 获得线程中保存的属性.
*
* @param attribute
* 属性名称
* @return 属性值
*/
public static Object get(String attribute) {
Map map = (Map) SESSION_MAP.get();
//System.out.println(map.toString());
// System.out.println(map.containsKey("usersession"));
return map.get(attribute);
}
/**
* 获得线程中保存的属性,使用指定类型进行转型.
*
* @param attribute
* 属性名称
* @param clazz
* 类型
* @param <T>
* 自动转型
* @return 属性值
*/
public static <T> T get(String attribute, Class<T> clazz) {
return (T) get(attribute);
}
/**
* 设置制定属性名的值.
*
* @param attribute
* 属性名称
* @param value
* 属性值
*/
public static void set(String attribute, Object value) {
Map map = (Map) SESSION_MAP.get();
if (map == null) {
map = new HashMap();
SESSION_MAP.set(map);
}
map.put(attribute, value);
}
8 另外在配置aop必须是接口的, 否则会出错
看是否可以在action设置拦截效果应该更好的。
分享到:
相关推荐
spring aop 拦截系统操作日志和异常日志,存储到数据库中的全过程
springaop拦截controller日志
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
SpringAOP的日志拦截示例, 包含配置和代码
spring mvc利用aop日志实现对controller层的拦截
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理, springMVC里做添加AOP拦截,用于捕获异常。
采用SpringAOP拦截Controller,Service实现操作日志管理,统一处理异常,登陆日志管理,是SpringAOP的应用实践。通过SpringAOP的处理,可以方便移植日志管理功能,是个不错的学习demo
用spring写的小示例程序,拦截某一层的所有方法。可以在调用之前、之后、抛出异常拦截调用,日志打印。
NULL 博文链接:https://conkeyn.iteye.com/blog/2354644
Spring Boot项目中使用Logback日志与使用AOP拦截请求日志信息
Spring之AOP在鉴权和日志记录中的应用
Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2
AopLogAopLog是基于SpringAop和ThreadLocal实现的一个对请求方法埋点记录与处理的日志工具包。设计目的和场景:使用Spring Aop拦截程序,基本上都是同一个小异,不想日后每个项目都柏林都写一份这样的Aop拦截处理...
ssh+aop+log4j+日志拦截器+注解
功能日志:基于java aop思想设计,通过Spring advisorAutoProxy(自动代理)实现controller过滤代理拦截,并提供拦截filter过滤,支持spring EL表达式。 数据日志:设计中提供数据日志注入接口,管理数据日志注入,...
本例是一个很好的springAop使用例子,例子举例了spring的前置通知和后置通知的使用方法。用以拦截操作日志。
讲解了3种AOP的开发方式,即使用@Aspect注释符、基于Schema的配置的开发方式和Spring API的开发方式,最后在实际的Java EE项目中实现5种Spring AOP功能(日志记录器、性能分析器、权限检查拦截器、异常监视器、在线...
用,留给您的简单任务就只是在一个 XML 配置文件中把它们连接好。 依赖注入是一个强大的特性,但是 Spring ...您很快就会看到,为了处理横切关注点,Spring AOP 发布了它自己的拦截器,您也可以编写您自己的拦截器。