1、添加其他jar包

2、配置applicationContext.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    <context:annotation-config />
    <context:component-scan base-package="com.fz.annotation" />
    <aop:aspectj-autoproxy />
     
</beans>

3、编写切面类LogInterceptor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.fz.annotation.aop;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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 void com.fz.annotation.service.impl.UserServiceImpl.userAdd(com.fz.xml.entity.User))")
    public void myMethod(){};
     
    @Before("myMethod()")
    public void before(){
        System.out.println("方法之前执行....");
    }
    @AfterReturning("myMethod()")
    public void afterRutting(){
        System.out.println("方法正常执行之后....");
    }
    @AfterThrowing("myMethod()")
    public void afterThrowing(){
        System.out.println("方法抛出异常之后....");
    }
    @Around("myMethod()")
    public void around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("方法执之前around......");//这里会优先于@Before先执行
        pjp.proceed();//向下继续方法的执行:(包括执行其他切面的拦截,如果当中抛出异常,则不会向下继续执行)
        System.out.println("方法执之后around......");//这里会在@AfterReturning执行之后执行
    }
}

Annotation方式实现AOP的更多相关文章

  1. Annotation方式配置AOP

    package com.xk.spring.kp04_aop.aop.s02_annotation; public interface IStudentService { public void sa ...

  2. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

  3. 菜鸟学习Spring——60s使用annotation实现简单AOP

    一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...

  4. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  5. 基于AspectJ的注解方式进行AOP开发

    -------------------siwuxie095                                     基于 AspectJ 的注解方式进行 AOP 开发         ...

  6. Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理

    1.加入DataSourceTransactionManager的命名空间 修改applicationContext.xml文件,增加如下内容: 1 2 3 4 5 6 7 8 9 10 11 12 ...

  7. 特性attribute,声明和使用attribute,应用attribute,AOP面向切面,多种方式实现AOP

    1 特性attribute,和注释有什么区别2 声明和使用attribute3 应用attribute4 AOP面向切面5 多种方式实现AOP ---------------------------- ...

  8. 一起学Spring之注解和Schema方式实现AOP

    概述 在上一篇,我们了解了通过实现接口和XML配置的方式来实现AOP,在实现注解方式AOP之前,先了解一下AspectJ.AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP语法,能 ...

  9. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

随机推荐

  1. linux系统上使用unzip命令

    最近在本地使用maven打包工程后,将工程部署到linux服务器的tomcat上,使用unzip解压工程报--->未找到命令.即该命名文件未安装,需要安装一下.安装命令如下: yum insta ...

  2. zookeeper 监听事件 CuratorWatcher

    zookeeper 监听事件 CuratorWatcher CuratorWatcher一次注册只监听一次,不监听查询. 1.监听测试类 package com.qy.learn.zk.curator ...

  3. LNMP环境简单教程

    一:LNMP可以进行简单优化,主要2方面.NGINX和PHP进程数,分别是以下2个文件: 1. /usr/local/nginx/conf/nginx.conf2. /usr/local/php/et ...

  4. 学号20145303 《Java程序设计》第一周学习总结

    学号20145303 <Java程序设计>第一周学习总结 教材学习内容总结 *dos命令行: dir:列出当前目录下的文件及文件名 md:创建目录 rd:删除目录.为空时文件夹(文件夹为空 ...

  5. 20145230熊佳炜《逆向及BOF基础实践》

    20145230熊佳炜<逆向及BOF基础实践> 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序同时包含另一个代码片段,getShell,会返回一个可用Shell. ...

  6. 配置hadoop集群的lzo压缩

    MR-Job中使用lzop详见MR案例:Job中使用Lzo压缩 1). 配置前的环境准备 # yum -y install lzo-devel zlib-devel gcc autoconf auto ...

  7. Covariance and Contravariance (C#)

    Covariance and Contravariance (C#) https://docs.microsoft.com/en-us/dotnet/articles/csharp/programmi ...

  8. Elasticsearch之几个重要的分词器

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch之中文分词器 Elasti ...

  9. git拉取GitLab工程报错Repository not found

    # git clone http://xxx/jiqing/frog.git 正克隆到 'frog'... fatal: repository 'http://xxx/jiqing/frog.git/ ...

  10. sprites.png雪碧图

    长时间不用把精灵图怎么用给忘了... 一.PC端 给所用到精灵图的元素设置background:url(sprites.png路径);  background-position: -x -y; 其中: ...