spring aop基于自定义注解做日志记录

本文介绍了一种利用Spring AOP实现系统操作日志记录的方法。通过自定义注解和切面,实现了对操作类型、模块及参数的自动记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.写一个日志自定义注解类

@Target({ElementType.PARAMETER, ElementType.METHOD})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public  @interface ArchivesLog {
    /** 要执行的操作类型比如:add操作 **/  
    public String operationType() default "";  

    /** 要执行的具体操作模块比如:车型管理 **/  
    public String operationModule() default "";

}

2.写一个日志处理类

@SuppressWarnings("rawtypes")
public class SystemLogAspect {
    @Autowired
    private  HttpServletRequest request;

    public void beforeMethod(JoinPoint joinPoint){  
    }  
    
    public  void afterMethod(JoinPoint joinPoint) { 
    String targetName = joinPoint.getTarget().getClass().getName();
    String methodName = joinPoint.getSignature().getName(); 
    Object[] arguments = joinPoint.getArgs(); //请求参数 
    String operationModule = "";     //操作模块
    String operationType=""; //操作类型
    
    Class targetClass = null;
    try {
targetClass = Class.forName(targetName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
    Method[] methods = targetClass.getMethods();
    for (Method method : methods) { 
    if (method.getName().equals(methodName)) {
    Class[] clazzs = method.getParameterTypes(); 
                if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(ArchivesLog.class)!=null{ 
    operationModule = method.getAnnotation(ArchivesLog.class).operationModule();
    operationType=method.getAnnotation(ArchivesLog.class).operationType();
                                break;  
        }
        }
        }

 }

3.在配置spring mvc的xml中配置我们的切面:

<!--将日志处理类注入到bean中。-->

<bean id="systemLogAspect" class="com.umltech.aop.SystemLogAspect"></bean>

(1)切指定方法的切面配置:

<aop:config>
        <!--  调用日志类   -->
        <aop:aspect  ref="systemLogAspect">
              <!-- 配置在controller包下所有的类在调用之前都会被拦截   -->
              <aop:pointcut id="logAdd" expression="(execution(* insert*(..)))||(execution(* edit*(..))))"/> 
              <!-- 方法前触发  -->
              <aop:before pointcut-ref="logAdd" method="beforeMethod"/> 
              <!-- 方法后触发 -->
                <aop:after pointcut-ref="logAdd" method="afterMethod"/>

            </aop:aspect>  

    </aop:config>

(2)切指定的类的切面配置:

<aop:config>

    <!--  调用用户访问统计类   -->

    <aop:aspect  ref="userAccessAspect">

         <!-- 配置在controller包下所有的类在调用之前都会被拦截   -->

    <aop:pointcut id="userAccessAdd" expression="execution(* com.umltech.controller..OperationStatController.*(..))"/> 

            <!-- 方法前触发  -->

            <aop:before pointcut-ref="userAccessAdd" method="beforeMethod"/> 

             <!-- 方法后触发 -->
             <aop:after pointcut-ref="userAccessAdd" method="afterMethod"/>

    </aop:aspect>  

</aop:config>

(3)切指定的包的切面配置

<aop:config>

         <!--  调用用户访问统计类   -->
          <aop:aspect  ref="userAccessAspect">
              <!-- 配置在controller包下所有的类在调用之前都会被拦截   -->
              <aop:pointcut id="userAccessAdd" expression="execution(* com.umltech.controller..*.*(..))"/> 
              <!-- 方法前触发  -->
              <aop:before pointcut-ref="userAccessAdd" method="beforeMethod"/> 
              <!-- 方法后触发 -->
                <aop:after pointcut-ref="userAccessAdd" method="afterMethod"/>

            </aop:aspect>  

</aop:config> 

4.在方法上面添加@ArchivesLog(operationType="add",operationModule="simManage") ,operationType为操作类型,operationModule为操作模块,这俩个在数据库中分别由俩张表进行维护

5.通过日志操作类我们就可以获取操作的模块,操作类型,操作参数,这样就可以创建日志了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值