天天看點

spring aop基于自定義注解做日志記錄

1.寫一個日志自定義注解類

@Target({ElementType.PARAMETER, ElementType.METHOD})  

@Retention(RetentionPolicy.RUNTIME)  

@Documented  

public  @interface ArchivesLog {

    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.通過日志操作類我們就可以擷取操作的子產品,操作類型,操作參數,這樣就可以建立日志了