在 xml中加

xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<aop:aspectj-autoproxy />

然后再要添加的方法前添加

@Aspect
@Component

在然后写织入点语法

@Pointcut("execution(public * com.bjsxt.service..*.add(..))")

public void myMethod(){};

@Before("myMethod()")

写添加方法

@Around("myMethod()")

写添加方法

例如:

package com.bjsxt.aop;

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

@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){};

@Before("myMethod()")
public void before() {
System.out.println("method before");
}

@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}

}

还有一种是把aop写在xml文件中

<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>

<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
</aop:aspect>

</aop:config>

package com.bjsxt.aop;

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

//@Aspect
//@Component
public class LogInterceptor {
//@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){};

//@Before("myMethod()")
public void before() {
System.out.println("method before");
}

//@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}

}

Spring AOP 整理的更多相关文章

  1. Spring AOP整理

    示例展示 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充.AOP之所以能得到广泛认可,主要是因为它将应用系统拆分分了 ...

  2. Spring AOP 整理笔记

    一.AOP概念 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 利用AOP可以对业务逻辑的各 ...

  3. AOP面试知识整理,^_^-包括spring Aop

    讲到java企业级开发框架,就不可避免的讲到 IOC,AOP,MCV 今天面试时被问到AOP,讲的很乱,这里整理笔记,包括AOP,spring-AOP的部分知识,错误的地方请小伙伴指出来. 谈谈你对A ...

  4. Spring AOP使用整理:各种通知类型的介绍

    2.PersonImpl类的源码 public class PersonImpl implements Person { private String name; private int age; p ...

  5. Spring AOP 知识整理

    通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...

  6. 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~

    Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...

  7. 利用反射手写代码实现spring AOP

    前言:上一篇博客自己动手编写spring IOC源码受到了大家的热情关注,在这里博客十分感谢.特别是给博主留言建议的@玛丽的竹子等等.本篇博客我们继续,还是在原有的基础上进行改造.下面请先欣赏一下博主 ...

  8. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  9. 【Spring】Spring AOP实现原理

    Spring AOP实现原理 在之前的一文中介绍过Spring AOP的功能使用,但是没有深究AOP的实现原理,今天正好看到几篇好文,于是就自己整理了一下AOP实现的几种方式,同时把代理模式相关知识也 ...

随机推荐

  1. Apache Ignite简介以及Ignite和Coherence、Gemfire、Redis等的比较

    一.Ignite简介 Apache Ignite 内存数组组织框架是一个高性能.集成和分布式的内存计算和事务平台,用于大规模的数据集处理,比传统的基于磁盘或闪存的技术具有更高的性能,同时他还为应用和不 ...

  2. Lock分析

      Lock接口是锁的实现,用来控制多个线程访问共享资源的方式,是在java 1.5的时候引入的,在此之前,只能通过synchronized的方式来取得对象的锁. synchronized中的锁是隐式 ...

  3. Linux系统启动流程与系统目录

    启动流程 # 1,开机BIOS自检 检查一系列的硬件,最后根据启动顺序启动,是硬盘还是光驱等 # 2,MBR引导 读硬盘0柱面0磁道1扇区的前446字节 1扇区=512字节 MBR=4446字节 四个 ...

  4. C# List泛型转换,int,string 转字符,转数组

    List转字符串 List<string> List = new List<string>(); string strArray = string.Join(",&q ...

  5. Linux: su sudo sudoer

    日常操作中为了避免一些误操作,更加安全的管理系统,通常使用的用户身份都为普通用户,而非root.当需要执行一些管理员命令操作时,再切换成root用户身份去执行. 普通用户切换到root用户的方式有:s ...

  6. sublime text安装插件

    http://www.sublimetext.com/ 安装Sublime Text 2插件的方法: 1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Package ...

  7. python‘s second day for me

    in     not in 主要用来检测一些字符串是否存在,或者避免一些字符串 while True: comment = input('请输入你的评论') if '顾清秋' in comment: ...

  8. postman 中url有动态变换的值时,可以按下面方式变换。

    get 和post均适用.

  9. 温故而知新-WebSocket 教程

    一.为什么需要 WebSocket? 初次接触 WebSocket 的人,都会问同样的问题:我们已经有了 HTTP 协议,为什么还需要另一个协议?它能带来什么好处? 答案很简单,因为 HTTP 协议有 ...

  10. 读《分布式一致性原理》CURATOR客户端

    创建会话 使用curator客户端创建会话和其它客户端产品有很大不同 1.使用CuratorFrameworkFactory这个工厂类的两个静态方法来创建一个客户端: public static Cu ...