Spring核心思想之一是AOP(Aspect Oriented Programming),即面向切面編程的技術, AOP讓開發人員專注核心業務,而通用邏輯則由專人使用 AOP 技術進行橫向切入處理,使得任務分離,提高開發和除錯的效率,應用于權限管理、日志管理等,
假設專案已完成Spring、SpringMVC配置,添加AOP應用
1.applicationContext.xml開啟支持AspectJ注解
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 自動掃描與裝配bean -->
<context:component-scan base-package="com.product"></context:component-scan>
<!-- 啟動對@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy proxy-target- />
</beans>
2.撰寫日志切面實作
2.1 定義日志注解類
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AppLog {
String value() default "";
}
2.2 日志功能
package com.product.aop;
import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 日志demo
*
*
*/
@Component
@Aspect
public class LogAspect {
private static final Log logger = LogFactory.getLog(LogAspect.class);
//抽取公共的切入點運算式
@Pointcut("@annotation(com.product.aop.AppLog)")
public void pointCut() {
};
//在目標方法之前切入
@Before("pointCut()")
public void logStart(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
System.out.println("" + joinPoint.getSignature().getName() + "運行以前:引數串列是:{" + Arrays.asList(args) + "}");
}
@After("com.product.aop.LogAspect.pointCut()")
public void logEnd(JoinPoint joinPoint) {
System.out.println("" + joinPoint.getSignature().getName() + "運行結束,");
}
// JoinPoint在引數表的第一位
@AfterReturning(value = "https://www.cnblogs.com/walkwithmonth/p/pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result) {
System.out.println("" + joinPoint.getSignature().getName() + "回傳運行結果:{" + result + "}");
}
@AfterThrowing(value = "https://www.cnblogs.com/walkwithmonth/p/pointCut()", throwing = "exception")
public void logException(JoinPoint joinPoint, Exception exception) {
System.out.println("" + joinPoint.getSignature().getName() + "例外,資訊:{" + exception + "}");
}
}
3.業務功能引入切面
import org.springframework.stereotype.Service;
import com.product.aop.AppLog;
@Service("userService")
public class UserService {
/**
* 標記日志注解
*/
@AppLog
public boolean exists(String userid,String name){
System.out.println("getUser");
return true;
}
}
4.測驗
瀏覽器請求http://127.0.0.1:8080/aop/user/submit.do?userid=001&name=lilei
控制臺輸出結果:
exists運行以前:引數串列是:{[001, lilei]}
getUser
exists運行結束,
exists回傳運行結果:{true}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/192479.html
標籤:Java
上一篇:springboot與elasticsearch整合
下一篇:JAVA是什么?
