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();
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。