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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • mysql查询语句执行过程及运行原理命令_MySQL常用命令

    mysql查询语句执行过程及运行原理命令_MySQL常用命令Mysql查询语句执行原理数据库查询语句如何执行?DML语句首先进行语法分析,对使用sql表示的查询进行语法分析,生成查询语法分析树。 语义检查:检查sql中所涉及的对象以及是否在数据库中存在,用户是否具有操作权限等 视图转换:将语法分析树转换成关系代数表达式,称为逻辑查询计划; 查询优化:在选择逻辑查询计划时,会有多个不同的表达式,选择最佳的逻辑查询计划; 代码生成:必须将逻辑查…

    2022年11月3日
  • 微信二维码登录原理是什么_请使用微信扫描二维码登录

    微信二维码登录原理是什么_请使用微信扫描二维码登录网页登陆是微信4.2以后版本提供的一种全新的登陆方式。用户只需要用手机扫一扫微信网页中的二维码,就能马上实现微信网页登陆。这种登陆方式虽然炫酷,但是多少有些违背直觉:网页端是怎么知道是哪个微信账号

    2022年8月4日
  • 真是好高兴,CSDN论坛也开了Blog!

    真是好高兴,CSDN论坛也开了Blog!    真的好高兴,我好久没来Csdn了,今天在网上搜资料时看到一篇文章也得很好,一看网址竟是Blog.csdn开头的,我才知道别人都已注册好久了。于是我也来激活了自己的Blog。感觉很不错!于是写了这第一篇文章!希望大家多多支持关注Csdn!愿他越来越兴旺!

    2022年10月2日
  • 数据预处理无量纲化处理_统计数据的预处理

    数据预处理无量纲化处理_统计数据的预处理1.无量纲化定义无量纲化,也称为数据的规范化,是指不同指标之间由于存在量纲不同致其不具可比性,故首先需将指标进行无量纲化,消除量纲影响后再进行接下来的分析。2.无量纲化方法无量纲化方法有很多,但

    2022年8月5日
  • 谓词表示法表示猴子摘香蕉_猴子妈妈有14个香蕉

    谓词表示法表示猴子摘香蕉_猴子妈妈有14个香蕉案例:我们要实现以下步骤:这个案例共有以下几种情况,猴子香蕉箱子在同一处,猴子香蕉在同一处,香蕉箱子在同一出,还有三者均不在同一处,但不论是哪种情况,我们需要清楚一点就算是香蕉和猴子在同一位置,猴子也无法直接获得香蕉,因此我们第一步必须需要先找到箱子,然后再去搬着箱子移动到香蕉处。本案例中有以下四个谓词逻辑: Run(monkey,box)代表猴子去搬箱子 Getbox(monkey,box)代表猴子得到了箱子 Run(monkey,banana)代表了.

    2022年9月26日
  • SpringCloud之Zuul网关[通俗易懂]

    SpringCloud之Zuul网关[通俗易懂]Zuul网关

    2022年8月15日

发表回复

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

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