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. 搭建solr集群的时候出现 ./zkcli.sh:行13: unzip: 未找到命令

    主要的原因是: linux系统下面没有安装压缩解压工具    zip 和 unzip:需要我们自己手动的安装: 利用yum命令安装即可: yum install -y unzip zip

  2. Mybatis入门(六)联查之一对多

    上一章说了多对一,很多学生被一个老师教,这一章是一个老师教很多学生 目录基本没有变化只是改了配置文件: 2.配置文件: TeacherMapper接口类: package com.hdlf.dao; ...

  3. Day3-D-Protecting the Flowers POJ3262

    Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When ...

  4. js隐藏button

    $("#plshbtn").attr("style","display:none;");

  5. JVM性能调优指南

    1.JVM的参数类型 1.1 标准参数:在各jdk版本中较稳定 -help -server -client -version -showversion -cp -classpath 1.2 X参数 1 ...

  6. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类

    interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...

  7. Wpa_supplicant 调试故障原因分析

    背景 在使用Wpa_supplicant 工具调试Linux的wifi的时候,发现有一些问题.特此记录一下.有些问题是遇到的并已经有了解决方法,一些问题比较发杂,只能作为思路. 问题以及解决办法 1. ...

  8. HihoCoder 1246:王胖浩与环

    #1246 : 王胖浩与环 时间限制:6000ms 单点时限:1000ms 内存限制:256MB 描述 王胖浩有一个环,环上有n个正整数.他有特殊的能力,能将环切成k段,每段包含一个或者多个数字. 对 ...

  9. 快速为Eclipse配置PyDev插件

    想学习Python,查询网络之后发现PyDev是很好的插件,所以就想为Eclipse配置它.结果在整个配置的过程中出现了各种问题,版本问题,重复问题,反正乱七八糟的,本身安装一次的时间就很长,中间出现 ...

  10. Easy_Re

    这题比较简单,一波常规的操作之后直接上ida(小白的常规操作在以前的博客里都有所以这里不在赘述了),ida打开之后查看一下, 这里应该就是一个入口点了,接着搜索flag字符串, 上面的黄色的部分转换成 ...