1. jar包

  

2.全局配置文件

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

        </beans>

3.1使用bean 的方式配置

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

       <bean id="before" class="com.aspect.Before"></bean>
       <bean id="tests" class="com.test.Tests"> </bean>
       <bean id="after" class="com.aspect.After"></bean>
       <bean id="final" class="com.aspect.Final"></bean>
       <bean id="exception" class="com.aspect.Exception"></bean>
        <aop:config>
            <aop:pointcut expression="execution(* com.test.Tests.say(*))" id="say"/><!-- 切入点 -->
            <aop:pointcut expression="execution(* com.test.Test.say1(*))" id="say1"></aop:pointcut>

            <aop:aspect ref="before">    <!-- 切入的方式(前后环异常最终)、方法 -->
                <aop:before method="dobefore" pointcut-ref="say"/>
            </aop:aspect>

            <aop:aspect ref="after">
                <aop:after-returning method="doafter" pointcut-ref="say" returning="obj"/>
            </aop:aspect>

            <aop:aspect ref="final">
                <aop:after method="dofinal" pointcut-ref="say"/>
            </aop:aspect>

        <!--     <aop:aspect ref="exception">
                <aop:after-throwing method="doexception" pointcut-ref="say" throwing="e"/>
            </aop:aspect> -->

        </aop:config>

        </beans>

  在java中创建对应的 切入方法

3.2 使用注解配置

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

   <context:component-scan base-package="com.aspect.auto"></context:component-scan>
       <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 

        </beans>

创建对应的切入方法,并使用注解:如:

package com.aspect.auto;

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

@Component
@Aspect
public class Around {

    @org.aspectj.lang.annotation.Around("within(com.aspect.auto.Says)")
    public void doAround(ProceedingJoinPoint jp){

        try {
            System.out.println("前置通知");
            jp.proceed();
            System.out.println(jp.proceed());
            System.out.println("后置通知");
        } catch (Throwable e) {
            System.out.println("异常通知:"+e.getMessage());
        }

        System.out.println("最终通知");

    }
}
package com.aspect.auto;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class After {

    @AfterReturning(pointcut="execution(* com..*.say(*))",returning="ret")
    public void after(JoinPoint jp,Object ret){
        System.out.println("执行后置通知");
        System.out.println("获得返回值:"+ret);
    }

}

测试 end

Spring AOP 基本的使用的更多相关文章

  1. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  2. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  3. spring aop

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...

  4. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  5. 基于Spring AOP的JDK动态代理和CGLIB代理

    一.AOP的概念  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...

  6. Spring AOP详解

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  7. Spring AOP实例——异常处理和记录程序执行时间

    实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...

  8. 从零开始学 Java - Spring AOP 实现用户权限验证

    每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...

  9. 从零开始学 Java - Spring AOP 实现主从读写分离

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  10. 从零开始学 Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

随机推荐

  1. 4、mysql查询练习

    1.创建四个表供查询 (1)学生表—Student 学号 姓名 性别 出生年月日 所在班级 [语句] > create table student( -> sno varchar(20) ...

  2. IdentityServer4专题之三:OAuth、SSO和OpenID

    一.oauth 典型案例:如果一个用户R拥有两项服务:一项服务是图片在线存储服务A,另一个是图片在线打印服务B.由于服务A与服务B是由两家不同的服务提供商提供的,所以用户在这两家服务提供商的网站上各自 ...

  3. python中单下划线的变量

    1._xxx 不能用于’from module import *’ 以单下划线开头的表示的是protected类型的变量.即保护类型只能允许其本身与子类进行访问.2.__xxx 双下划线的表示的是私有 ...

  4. 六种方式实现hibernate查询,及IDE推荐

      这些天过的好乱,也许是因为考完试了,心里有些松懈吧.也许是最近发生的事对我有些触动吧.感觉自己都已经不懂自己了.面对一些人的教导,我很感激.因为很多话都对我有非常大的帮助和启发,也让我除了做技术, ...

  5. 三 进制、精度,Java的类型转换

    进制的表示: 0b010 :  二进制表示形式:前面+0n 0100  : 八进制表示形式: 前面+0 0x001 : 16进制表示形式:前面+0x 计算机以补码的方式进行运算 进制的转换: 10进制 ...

  6. 攻防世界web新手区(3)

    xff_referer:http://111.198.29.45:43071 打开网址,显示出这个页面: X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP, ...

  7. CF 1198 A. MP3 模拟+滑动窗口

    A. MP3   题意:给你n个数,一个大小为8*I的容量,保存一个数需要多少容量取决于给定n个数的种类k,用公式 log2 k   计算,如果给定的容量不能保存所有数,选择减少数的种类来降低保存一个 ...

  8. springboot官网->pom.xml文件

    springboot 2.1.6 pom.xml

  9. 基于线程池、消息队列和epoll模型实现并发服务器架构

    引言 并发是什么?企业在进行产品开发过程中为什么需要考虑这个问题?想象一下天猫的双11和京东的618活动,一秒的点击量就有几十万甚至上百万,这么多请求一下子涌入到服务器,服务器需要对这么多的请求逐个进 ...

  10. 和为S的连续正整数序列(双指针法)

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...