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


相关推荐

  • Script:Datafile Report

    Script:Datafile Report

    2021年7月27日
    61
  • postman调试rpc接口

    postman调试rpc接口使用postman测试RPC接口PostMan如何调用RPC接口(postman下载)PostMan如何调用RPC接口接口调试之Postman使用方法详解

    2022年10月13日
    2
  • [渝粤教育] 徐州工业职业技术学院 橡胶原材料 参考 资料「建议收藏」

    [渝粤教育] 徐州工业职业技术学院 橡胶原材料 参考 资料「建议收藏」教育-橡胶原材料-章节资料考试资料-徐州工业职业技术学院【】课程认知随堂测验1、【多选题】下列制品可采用橡胶材料制作的是。A、轮胎B、鞋子底C、输送带D、婴儿奶嘴参考资料【】2、【多选题】硫化体系主要包括。A、硫化剂B、促进剂C、活性剂D、防焦剂参考资料【】3、【判断题】橡胶是一种材料,它在大的形变下能迅速而有力恢复其形变,能够被改性(硫化)。A、正确B、错误参考资料【】4、【判断题】生胶是一种高弹性高聚物材料,是制造橡胶制品的基础材料,一

    2022年10月2日
    3
  • 【获奖公布】“我的2016”主题征文活动

    【获奖公布】“我的2016”主题征文活动还记得2015的年末,2016的新年伊始,你给自己定下的目标,对自己许下的诺言么?时光荏苒,一年又在指缝间溜走了,离2016的结束还剩十多天,在接下来的这十几天里,让我们用博客记录下这或开心、或痛苦,或特殊,或平淡的2016年,愿剩下的每一天我们都不会虚度~值此岁末之际,CSDN博客频道携手“图灵教育”开展了“我的2016”主题征文活动,听大家聊聊2016年的工作、生活中的点滴感动、喜悦和收获,

    2022年6月21日
    23
  • php openssl 怎么升级,OpenSSL升级和php加扩展模板openssl

    php openssl 怎么升级,OpenSSL升级和php加扩展模板openssl升级 openssl 版本 以版本 1 0 2l 为例子方法如下 1 下载最新版本的 openssl 源码包 wgetftp ftp openssl org source openssl 1 0 2l tar gzhttps www openssl org source 2 安装 openssl1 tar xzvfopenssl 1 0 2l tar gz2 cdopenssl 1 0 2l

    2025年7月1日
    2
  • 学英语网络资源推荐

    学英语网络资源推荐1.推荐标准和目的2.中国十佳英语学习网站推荐3.学英语Web资源推荐4.在线辞典大全5.语法网站列表6.英语新闻网站大全7.英文电影网站大全返回1.推荐标准和目的希望能对了解英语(文化)不多的国内英语初学者(尤其是社会人士)有所帮助。希望打开初学者利用Internet学习英语的…

    2022年5月25日
    35

发表回复

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

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