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. Mac安装jdk

    jdk:https://blog.csdn.net/zw235345721/article/details/78702254 mysql:https://www.jianshu.com/p/fd3aa ...

  2. Ajax--XMLHttpRequest的使用

    1.创建XMLHttpRequest对象(实现方法不统一): --IE把XMLHttpRequest实现为一个ActiveX对象: --其他浏览器(Firefox.Chrome等)把它实现为一个本地的 ...

  3. 前端学习笔记系列一:8 <noscript>…</noscript>,网站路径,vscode跳出右括号

    1.<noscript>…</noscript> 在body中使用此段代码,可识别 <script> 标签但无法支持其中的脚本的浏览器. 此段代码意思为如果浏览器不 ...

  4. 给服务器做pve系统(可以通过web管理物理机集群资源与虚拟机)

    做此系统前,可以先进入bios,设置一下ipmi的网络地址.可以远程管理服务器 输入服务器的ipmi里面配置的ip 默认账号与密码admin 点击launch 会自动下载认证文件 下载好java软件环 ...

  5. 今日份学习:写一些代码 (Spring+AOP+Redis+MySQL练习)

    笔记 Spring+AOP+Redis+MySQL练习 1. 启动docker->mysql docker run --name mysql -v e:\docker:/var/lib/mysq ...

  6. windows驱动不要签名

    BCDEDIT -SET LOADOPTIONS DISABLE_INTEGRITY_CHECKSBCDEDIT -SET TESTSIGNING ON

  7. mysql创建数据库并设置字符集编码

    create database `mydb` character set utf8 collate utf8_general_ci;

  8. 无聊看看c是咋写游戏的

    /* 项目名称:五子棋, vs */ /* 绘图环境,初始化游戏 1.绘图环境(窗口) 2.背景图片 3.背景音乐 4.绘制棋盘 */ #include"pch.h" #inclu ...

  9. HTML文本域(文本框)禁止修改写入数据方法

    html文本域有时需要禁止修改内容,方法如下: 加入readonly=""或readonly="readonly" 如下:<input name=&quo ...

  10. Golang的运算符-位运算符

    Golang的运算符-位运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.位运算符概述 常见的位逻辑运算符: &: 位与运算符,表示AND(表示所有条件都得匹配), ...