Spring AOP入门使用详解

Spring AOP入门使用详解AOP入门详解

大家好,又见面了,我是你们的朋友全栈君。

1.maven 依赖:

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--aop-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>

2.接口和实现定义

接口定义:

package com.aop.api;

/**
 * Created by zhangzh on 2016/8/5.
 */
public interface Service {
    public void save(String info);
}

接口实现:

package com.aop.impl;

import com.aop.api.Service;

/**
 * Created by zhangzh on 2016/8/5.
 */
public class ServiceImpl implements Service {
    public void save(String info) {
        System.out.println("save info:" + info);
    }
}

 

3.采用before和after方式进行切面操作:

切面类 

package com.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Created by zhangzh on 2016/8/5.
 */
public class aspect {

    public void before(JoinPoint call) {

        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
       Object[] args = call.getArgs();
        for(Object obj:args) {
            if(obj instanceof String) {
                System.out.println(methodName +"的执行参数为:" + obj);
            }
        }

        System.out.println("前置通知:" + className + "类的" + methodName + "开始执行了......");

    } 
    public void after() {
        System.out.println("最终通知:不管方法有没有正常执行完成,一定会返回的");
    }
}

aopContext.xml配置:

  

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="service" class="com.aop.impl.ServiceImpl"/>
    <bean id="aspect" class="com.aop.aspect.aspect"/>

    <aop:config>
        <aop:aspect id="logAspect" ref="aspect">
            <aop:pointcut id="allMethod" expression="execution(* com.aop.api.Service.*(..))"/>
            <aop:before method="before" pointcut-ref="allMethod"/>
            <aop:after method="after" pointcut-ref="allMethod"/>
            <!--<aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>-->
            <!--<aop:around method="doAround" pointcut-ref="allMethod"/>-->
        </aop:aspect>
    </aop:config>


</beans>

test类:

import com.aop.api.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by zhangzh on 2016/8/5.
 */
public class AopTest {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/aopContext.xml");

        Service service = ac.getBean("service",Service.class); 
        service.save("### 1232536 #########");



    }
}

输出结果:

save的执行参数为:### 1232536 #########
前置通知:com.aop.impl.ServiceImpl类的save开始执行了......
save info:### 1232536 #########
最终通知:不管方法有没有正常执行完成,一定会返回的

4. 采用环绕aop配置:

切面类:

package com.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Created by zhangzh on 2016/8/5.
 */
public class aspect {

    public void before(JoinPoint call) {

        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
       Object[] args = call.getArgs();
        for(Object obj:args) {
            if(obj instanceof String) {
                System.out.println(methodName +"的执行参数为:" + obj);
            }
        }

        System.out.println("前置通知:" + className + "类的" + methodName + "开始执行了......");

    }

    public void afterReturn() {
        System.out.println("后置通知:方法正常结束了");
    }

    public void after() {
        System.out.println("最终通知:不管方法有没有正常执行完成,一定会返回的");
    }

    public void afterThrowing() {
        System.out.println("异常抛出后通知:方法执行时出异常了");
    }

    //用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型
    public Object doAround(ProceedingJoinPoint call) throws Throwable {
        Object result = null;
        this.before(call);//相当于前置通知
        try {
            result = call.proceed();
            this.afterReturn(); //相当于后置通知
        } catch (Throwable e) {
            this.afterThrowing();  //相当于异常抛出后通知
            throw e;
        } finally {
            this.after();  //相当于最终通知
        }
        return result;
    }

}

aopContext.xml配置:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="service" class="com.aop.impl.ServiceImpl"/>
    <bean id="aspect" class="com.aop.aspect.aspect"/>

    <aop:config>
        <aop:aspect id="logAspect" ref="aspect">
            <aop:pointcut id="allMethod" expression="execution(* com.aop.api.Service.*(..))"/>
            <!--<aop:before method="before" pointcut-ref="allMethod"/>-->
            <!--<aop:after method="after" pointcut-ref="allMethod"/>-->
            <!--<aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>-->
            <aop:around method="doAround" pointcut-ref="allMethod"/>
        </aop:aspect>
    </aop:config>


</beans>

test类:

import com.aop.api.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by zhangzh on 2016/8/5.
 */
public class AopTest {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/aopContext.xml");

        Service service = ac.getBean("service",Service.class); 
        service.save("### 1232536 #########");



    }
}

输出结果:

save的执行参数为:### 1232536 #########
前置通知:com.aop.impl.ServiceImpl类的save开始执行了......
save info:### 1232536 #########
后置通知:方法正常结束了
最终通知:不管方法有没有正常执行完成,一定会返回的

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149583.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 小米平板5采用120Hz高刷LCD屏:纯平中框+侧面指纹[通俗易懂]

    小米平板5采用120Hz高刷LCD屏:纯平中框+侧面指纹[通俗易懂]根据此前官方透露的消息,久未更新的小米平板产品线将在近期得到更新,将推出全新一代小米平板5系列产品,并且号称将配备前所未有的旗舰配置。随着发布时间的日益临近,关于该机的爆料也越来越密集。现…

    2022年6月20日
    27
  • 关于lvm扩容的方式「建议收藏」

    关于lvm扩容的方式「建议收藏」服务器磁盘扩容在项目上很常见,这里总结下常见的几种lvm扩容的方式供大家参考。

    2022年6月20日
    73
  • goland激活服务器(注册激活)

    (goland激活服务器)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html23LNPMIJZT-eyJsaWNlbnNlSWQi…

    2022年3月29日
    271
  • 推荐系统中TopN与kNN的区别

    推荐系统中TopN与kNN的区别

    2022年2月22日
    40
  • FOC入门教程_晚上开飞机前面有灯吗

    FOC入门教程_晚上开飞机前面有灯吗深入浅出FOC(FieldOrientedControl)前言:为什么要学习FOC?1.电机控制是自动化控制领域重要一环。2.目前直流无刷电机应用越来越广泛,如无人机、机械臂、云台、仿生机器人等等。3.电机控制工程师薪水较高。需要什么基础?1.C语言,指针,结构体,编程规范。2.STM32外设使用。3.原理图阅读。4.芯片手册阅读。5.数序坐标系知识为什么要出本教程?1.直流无刷电机应用越来越广泛,网上资料比较散落,因此想要出一篇系统性的教程,从头到尾,深入浅出,帮助初学者快速

    2025年8月4日
    3
  • linux tar 绝对路径,tar打包使用绝对路径详解

    linux tar 绝对路径,tar打包使用绝对路径详解首先应该明确:*nix系统中,使用tar对文件打包时,一般不建议使用绝对路径。通常是在两台环境相似的机器上进行同步复制的时候,才有需要使用绝对路径进行打包。使用绝对路径打包时如果不指定相应的参数,tar会产生一句警告信息:”tar:Removingleading`/’frommembernames”,并且实际产生的压缩包会将绝对路径转化为相对路径。比如:root@queen~#t…

    2022年5月24日
    62

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号