Spring AOP的简单示例

配置文件

复制代码
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
    <context:component-scan base-package="com.zhiguoguo.service,aspect"/>

    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>
复制代码


service类:

复制代码
package com.zhiguoguo.service;

import org.springframework.stereotype.Component;

@Component
public class HelloService {
    public String sayHello() {
        System.out.println("——————方法执行——————");
//        throw new RuntimeException("haha");
        return "Hello world!";
    }
}
复制代码


切面AOP:

复制代码
package com.zhiguoguo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 定义切面
 */
@Component
@Aspect
public class HelloServiceAspect {

    @Before("execution(* com.zhiguoguo.service.HelloService.*(..))")
    public void authorith(){
        System.out.println("模拟进行权限检查。");
    }

    @After("execution(* com.zhiguoguo.service.HelloService.*(..))")
    public void release() {
        System.out.println("模拟方法结束后的释放资源...");
        System.out.println();
    }

    @AfterReturning(returning="obj", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))")
    public void log(Object obj) {
        System.out.println("模拟目标方法返回值:" + obj);
        System.out.println("模拟记录日志功能...");
        System.out.println();
    }

    @AfterThrowing(throwing="ex", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))")
    public void doRecoverActions(Throwable ex) {
        System.out.println("目标方法中抛出的异常:" + ex);
        System.out.println("模拟抛出异常后的增强处理...");
    }

//    @Around("execution(* com.zhiguoguo.service.HelloService.*(..))")//这里先注释掉
    public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
        System.out.println("执行目标方法之前,模拟开始事物...");
        // 执行目标方法,并保存目标方法执行后的返回值
        Object rvt = jp.proceed();

        //这里的切面方法如果有参数才能传递一个参数给他
//        Object rvt = jp.proceed(new String[]{"被改变的参数"});

        System.out.println("执行目标方法之前,模拟结束事物...");
        System.out.println();

        //经过这么一个环绕,可以增强返回值
        return rvt + "新增的内容";
    }


}
复制代码


主函数:

复制代码
package main;

import com.zhiguoguo.service.HelloService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        HelloService helloService = context.getBean(HelloService.class);
        helloService.sayHello();
    }
}
复制代码

 

posted @   无忧之路  阅读(352)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2013-11-26 Hibernate 缓存 关于注解方式
无忧之路
点击右上角即可分享
微信分享提示