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. 前端面试题CSS一(题目来源网络)

    一.什么是html5语义化? 使用合理,正确的html标签格式化文档. 二.CSS样式优先级? 就近原则,行内>内联>外联 三 什么是盒模型? 主要分为两种,w3c标准盒模型,IE标准模型 ...

  2. UOJ192 最强跳蚤

    题目链接 problem 给出一个n个点带边权的树,问有多少对\((u,v)\)满足\(u\)到\(v\)路径上边权的乘积为完全平方数. \(n\le 10^5,w\le 10^8\) solutio ...

  3. spingboot中使用scheduled设置定时任务注意事项

    在spring开发过程中经常会遇到需要定时执行的任务,如定时生成报表,定时推送消息等任务. springboot 提供了简单的 @Scheduled 表达式来配置定时任务.该方式默认是单线程的,任务在 ...

  4. log4j, common-logging, slf4j 关系

    最近因为项目原因,认真学习了一下 log4j 相关内容,主要是从网上找资料,以及追踪原代码.   关于如何使用,网上有很多资料,这里不做具体介绍.下面介绍一下这些工具的关系.   log4j 是最强大 ...

  5. 多实例mysql的安装和管理

    多实例mysql的安装和管理 http://blog.chinaunix.net/uid-20639775-id-3438560.html mysql的多实例有两种方式可以实现,两种方式各有利弊.第一 ...

  6. leetcode844 Backspace String Compare

    """ Given two strings S and T, return if they are equal when both are typed into empt ...

  7. TeX 常用命令记录

    常用: $a_{i}^{2}$    a_{i}^{2} $\sqrt x$     $\sqrt[n] x$     \sqrt[n] x $\frac{1+2}{3+4}$ 重音符号: $\hat ...

  8. 如何看Analysis分析图

    第一步,从分析Summary的事务执行情况入手. Summary主要是判定事务的响应时间与执行情况是否合理.如果发现问题,则需要作进一步分析.通常情况下,如果事务执行情况失败或者响应时间过长等,都需要 ...

  9. LoadRunner回放脚本时,显示浏览器的设置

    打开LoadRunner的VuGen,选择Tools-->General Options-->Display,在Display里将 Show browser during replay打钩 ...

  10. zabbix监控memcached服务

    zabbix监控memcached服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装并配置memcached服务 1>.使用yum方式安装memcached [ro ...