spring aop 统一捕获异常和写日志

本文介绍如何使用Spring AOP实现异常捕获及日志记录功能,包括配置步骤、代码示例等内容。

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

首先,什么是spring AOP你可以去参考网上的其他文章,这边我将给您介绍用spring AOP 实现的异常捕获和日志的小demo,我也会详细解释相关配置。

首先给大家看一下我的工程目录:


大家可以先用eclipse中新建一个maven工程,在工程中pom.xml按下面文件添加依赖:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zy</groupId>
  <artifactId>AOPException</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>AOPException Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.5.2</version>
    </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.6.RELEASE</version>
    </dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>5.0.6.RELEASE</version>
	</dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-annotations</artifactId>
	    <version>2.9.5</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-core</artifactId>
	    <version>2.9.5</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-databind</artifactId>
	    <version>2.9.5</version>
	</dependency>
	<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>5.0.6.RELEASE</version>
	    <scope>test</scope>
    </dependency>
	<dependency>
	    <groupId>org.mockito</groupId>
	    <artifactId>mockito-core</artifactId>
	    <version>2.18.3</version>
	    <scope>test</scope>
	</dependency>
  </dependencies>
  <build>
    <finalName>AOPException</finalName>
  </build>
</project>

添加完依赖后,开始配置web.xml文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>exception-test-web</display-name>
     
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>//这边是加载spring的配置文件
            /WEB-INF/config/applicationContext.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>production</param-value>
    </context-param>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
//这边是加载spring的配置文件
<param-value>/WEB-INF/config/spring-mvc-config.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>20</session-timeout> </session-config></web-app>

配置完web.xml后,你可开始配置spring配置文件了,也就是applicationContext.xml文件。如下:

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
	default-lazy-init="true">
	<description>Spring公共配置</description>

	<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
	
    <context:component-scan base-package="com.zy.test">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />//不扫描controller注解类
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />//不扫描ControllerAdvice注解类
    </context:component-scan>
    
</beans>

然后配置spring-mvc-config.xml文件,如下:

<?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:mvc="http://www.springframework.org/schema/mvc"  
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">
	<!-- 自动扫描 -->
	<context:component-scan base-package="com.zy.test" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />//扫描COntroller注解类
		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />//扫描ControllerAdvice注解类
	</context:component-scan>
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg value="UTF-8" />
				<property name="writeAcceptCharset" value="false" />
			</bean>
			<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="prettyPrint" value="true" />
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	<!--  
	 <bean class="com.zy.test.config.ExceptionConfig"/>
	-->
       <!-- 这里的配置可以通过 com.zy.test.config.ExceptionConfig这个注解类来完成 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <bean class="com.zy.test.aspect.WebExceptionAspect" scope="singleton"/> //配置了单例
   
</beans>

上面spring容器和spring-mvc容器中扫描的类不一样,是为了区分两个容器的职责,spring-mvc负责controller控制器这块的类。因为我们的异常捕获是从controller类往外抛,所以我们对于aspect类也放在spring-mvc容器中。

下面就开始介绍我们的切面类:

package com.zy.test.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
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;

import com.zy.test.annotation.ArchivesLog;

/**
 * web 异常切面
 * @author user
 *
 */
@Aspect
public class WebExceptionAspect {
    
    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")//连接点是@RequestMapping注解的方法
    private void webPointcut() {}
    
    @AfterThrowing(pointcut = "webPointcut()", throwing = "e")//切点在webpointCut()
    public void handleThrowing(JoinPoint joinPoint, Exception e) {//controller类抛出的异常在这边捕获
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //开始打log
        System.out.println("异常:" + e.getMessage());
        System.out.println("异常所在类:" + className);
        System.out.println("异常所在方法:" + methodName);
        System.out.println("异常中的参数:");
        System.out.println(methodName);
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i].toString());
        }
    }
    

    @Before("execution(* com.zy.test.controller.*.*(..))")
    public void beforeProcess(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //在项目中最好记录当前操作的时间和用户
        System.out.println("操作所在类:" + className);
        System.out.println("操作所在方法:" + methodName);
        System.out.println("操作中的参数:");
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i].toString());
        }
    }
    
    @AfterReturning(value = "execution(* com.zy.test.controller.*.*(..)))", returning = "returnVal")
    public void returnProcess(JoinPoint joinPoint, Object returnVal) {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Class targetClass = null;
        String operationName = "";
        try {
            targetClass = Class.forName(className);
        } 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 == args.length&&
                        method.getAnnotation(ArchivesLog.class)!=null) {//这块是取出我们注解ArchiveLog中的值,一遍在日志时明确这个操作的名称
                    operationName = method.getAnnotation(ArchivesLog.class).operationName();
                    break;
                }
            }
        }
        System.out.println("操作名称:" + operationName);
        System.out.println("方法正常返回的值:" + returnVal);
    }
    
}

这边我们需要自定义注解:

package com.zy.test.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;

@Target({ElementType.PARAMETER, ElementType.METHOD})  //注解可以用于参数或者方法上
@Retention(RetentionPolicy.RUNTIME)  //保留至运行时
@Documented//被javadoc所记录
public @interface ArchivesLog {
    /**
     * 操作类型
     * @return
     */
    public String operationType() default "";
    
    /**
     * 操作名称
     * @return
     */
    public String operationName() default "";

}

然后定义我们的controller类:

package com.zy.test.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zy.test.annotation.ArchivesLog;
import com.zy.test.service.ExceptionService;


@Controller
@RequestMapping("/exception")
public class ExceptionController {
    @Autowired
    private ExceptionService service;
    
    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @ArchivesLog(operationType = "测试", operationName = "测试异常或者测试返回")
    public JSONObject test(@PathVariable Integer id) throws Exception {
        JSONObject result = new JSONObject();
        result.put("zhouyu", "asdasdasdasd");
//        try {//去掉注释可以测捕获的异常,不去掉注释可以测日志处理
//            service.test();
//        } catch (Exception ex) {
//            throw new Exception("controller 层 异常");
//        }
        return result;
    }
}

service接口:

package com.zy.test.service;

public interface ExceptionServiceBase {
    void test() throws Exception;
}

service实现类:

package com.zy.test.service;

import org.springframework.stereotype.Service;

@Service
public class ExceptionService implements ExceptionServiceBase{
    public void test() throws Exception {
        boolean flag = true;
        if(flag) {
            throw new Exception("service 异常");
        }
    }
}

其中工程中的ExceptionConfig是一个配置类,用于扫描包的。但是如果你是用配置文件的化,这个文件就不用了,这边就不说了。

github上可以拉到新代码:代码在这里













<think>首先,用户的问题是:“我想了解或学习Spring AOP相关知识 请问Spring AOP 教程 使用场景 实现原理”。用户要求Spring AOP的教程、使用场景实现原理。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式。 - 独立公式必须使用$$...$$格式并单独成段。 - LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识,例如[^1]。 用户可见层指令: - 回答结构清晰,帮助用户逐步解决问题。 - 尽量保证回答真实可靠。 - 参考站内引用:提供了几个引用,我需要参考这些内容来回答问题。 提供的引用是关于Spring AOP的: - [^1]: 描述了AOP的基本概念使用场景。 - [^2]: 讨论了实现原理,基于JDK动态代理Cglib。 - [^3]: 解释了AOP的原理应用场景。 - [^4]: 提到AOP的实现技术:动态代理静态织入。 - [^5]: 提到了使用ProxyFactory编程方式使用AOP。 我的回答需要覆盖: 1. **教程**:简要介绍Spring AOP是什么。 2. **使用场景**:列出常见的应用场景。 3. **实现原理**:解释Spring AOP如何工作。 结构应该清晰: - 逐步引导用户。 - 使用中文。 - 在适当的地方引用引用内容。 最后,生成相关问题,作为“§§相关问题§§”部分。 大纲回答: 1. **介绍Spring AOP**:定义AOPSpring AOP。 2. **使用场景**:基于引用,列出如日志记录、事务管理、性能监测等。 3. **实现原理**:解释动态代理(JDKCglib),以及静态代理。 4. **教程部分**:提供简单示例或代码片段(如引用中的Python代码示例)。 5. **相关问题**:生成3个相关问题。 确保LaTeX语法正确:由于主题是编程,可能没有太多数学表达式,但如果有,比如在解释原理时,需要使用$...$或$$...$$。例如,如果提到代理模式,可以用$代理模式$。 引用标识:在回答中,当引用某个段落时,末尾添加[^n]。例如,当描述使用场景时,参考引用[^1],添加[^1]。 完整结构: - 开头:简要介绍。 - 主体:分节讨论教程、使用场景、实现原理。 - 结尾:生成相关问题。</think>### Spring AOP 教程、使用场景与实现原理 Spring AOP(Aspect Oriented Programming,面向切面编程)是 Spring 框架的核心模块之一,用于处理横切关注点(cross-cutting concerns),即那些分散在多个业务逻辑中的公共功能(如日志记录、事务管理)。通过 AOP,开发者可以将这些功能模块化,避免代码重复,提升可维护性。下面我将逐步介绍 Spring AOP 的教程、使用场景实现原理,确保内容清晰可靠。 #### 1. Spring AOP 教程 Spring AOP 的核心是“切面”(Aspect),它由“切入点”(Pointcut)“通知”(Advice)组成: - **切入点(Pointcut)**:定义在哪些方法或类上应用切面逻辑(例如,所有以 `save` 开头的方法)。 - **通知(Advice)**:在切入点指定的位置执行的代码逻辑,包括: - `@Before`:方法执行前执行。 - `@After`:方法执行后执行(无论成功或失败)。 - `@Around`:环绕方法执行(可在方法前后添加逻辑)。 - `@AfterReturning`:方法成功返回后执行。 - `@AfterThrowing`:方法抛出异常后执行。 一个简单的 Spring AOP 教程示例(使用 Java Spring Boot): ```java // 1. 定义切面类 @Aspect @Component public class LoggingAspect { // 2. 定义切入点:所有 service 包下的方法 @Pointcut("execution(* com.example.service.*.*(..))") public void serviceMethods() {} // 3. 定义通知:在方法执行前打印日志 @Before("serviceMethods()") public void logBefore(JoinPoint joinPoint) { System.out.println("方法执行前: " + joinPoint.getSignature().getName()); } } // 4. 在 Spring Boot 主类启用 AOP @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` - **步骤说明**: 1. 使用 `@Aspect` 注解标记切面类。 2. 通过 `@Pointcut` 指定切入点表达式(这里匹配 `service` 包下的所有方法)。 3. 使用 `@Before` 通知添加日志逻辑。 4. 在配置类启用 AOP 支持(`@EnableAspectJAutoProxy`)。 - **优势**:无需修改业务代码,即可统一添加功能。例如,上述代码会在所有 service 方法执行前打印日志[^1]。 #### 2. 使用场景 Spring AOP 适用于需要将横切逻辑从核心业务中分离的场景,常见应用包括: - **日志记录**:统一记录方法调用信息(如参数、执行时间),避免在每个方法中重复日志代码[^1]。 - **事务管理**:在数据库操作前后自动开启/提交事务,确保数据一致性(例如,Spring 的 `@Transactional` 注解基于 AOP 实现)[^3]。 - **性能监测**:统计方法执行时间,用于性能优化(如使用 `@Around` 通知计算耗时)[^3]。 - **访问控制**:在方法执行前检查权限(例如,基于角色验证用户权限)[^3]。 - **缓存管理**:在方法返回结果后自动缓存数据,减少重复计算[^3]。 - **异常处理**:统一捕获并处理异常(如发送警报或回滚事务)[^1]。 这些场景的核心是“横切逻辑”,即那些影响多个模块的非核心功能。AOP 通过“切面”将其模块化,提升代码复用性可维护性[^1]。 #### 3. 实现原理 Spring AOP 的实现基于动态代理技术,在运行时生成代理对象来拦截方法调用。其原理可分为两类: - **动态代理**:Spring 默认在运行时动态创建代理类。 - **JDK 动态代理**:如果目标类实现了接口,Spring 使用 JDK 的 `java.lang.reflect.Proxy` 生成代理。代理对象实现相同接口,并在方法调用时插入通知逻辑。例如,目标方法 `save()` 被调用时,代理会先执行 `@Before` 通知[^2]。 - **Cglib 动态代理**:如果目标类未实现接口,Spring 使用 Cglib 库生成子类代理。Cglib 通过继承目标类,并重方法来实现拦截(性能略低于 JDK 代理,但更灵活)[^2]。 动态代理的决策由 `AopProxyFactory` 基于目标对象配置自动选择:接口存在时用 JDK 代理,否则用 Cglib[^2]。数学上,代理过程可视为一个拦截函数:设目标方法为 $f(x)$,代理逻辑为 $g(x)$,则实际调用为 $g(f(x))$ 或 $f(g(x))$,取决于通知类型(如 `@Around` 是 $g(f(x))$)。 - **静态织入**:在编译期修改字节码(如使用 AspectJ 编译器),但 Spring AOP 主要依赖动态代理,静态织入较少使用[^4]。 **关键流程**: 1. **代理创建**:Spring 容器启动时,检测 `@Aspect` 类,并基于切入点匹配目标 Bean。 2. **代理生成**:通过 `ProxyFactory` 动态创建代理对象(编程方式也可直接使用 `ProxyFactory` 设置目标通知)[^5]。 3. **方法拦截**:当调用目标方法时,代理对象先执行通知链(如 `@Before` → 目标方法 → `@After`)。 4. **性能考虑**:动态代理在内存中生成,开销较小,适用于大多数应用;静态织入性能更高,但需额外工具支持[^3]。 Spring AOP 的优势在于简化了横切逻辑的管理,但其作用域限于 Spring 管理的 Bean,不适用于非 Spring 对象[^2]。 ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值