之前是通过配置完成aop操作,如果自己写的话,太麻烦了,可以使用基于annotation的配置完成。

第一步:打开AOP的annotation支持

加上一句话:

    <context:annotation-config/>
<context:component-scan base-package="com.Spring"/>
  <aop:aspectj-autoproxy  proxy-target-class="true"/>    //使用了jdk的自动动态代理,需要加上这句话,否则报错
<aop:aspectj-autoproxy/>

随后需要在ServiceAspect类中编写所需要使用的annotation。

范例:修改serviceAspect类。

package com.Spring.aop;

import java.lang.reflect.Array;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.springframework.stereotype.Component; import com.Spring.Vo.Member; @Component  //用来取代在xml中配置bean初始化
@Aspect   //用来代替在xml中配置AOP操作
public class ServiceAspect {
@Before(value="execution(* com.Spring..*.*(..)))")
public void serviceBefore()
{
System.out.println("AOP切面执行日志记录操作");
}
@Before(value="execution(* com.Spring..*.*(..)) and args(param)))",argNames="param")
public void serviceBefore2(Object arg)
{
System.out.println("AOP切面执行增加前操作,参数=" +arg);
}
@After(value="execution(* com.Spring..*.*(..)))")
public void serviceAfter()
{
System.out.println("AOP切面执行事务处理操作");
}
@AfterReturning(value="execution(* com.Spring..*.*(..)))",argNames="ret",returning="ret")
public void serviceAfterReturn(Object val) //表示操作结果
{
System.out.println("AOP切面操作完成,返回结果:"+val);
}
@AfterThrowing(value="execution(* com.Spring..*.*(..)))",argNames="e",throwing="e")
public void serviceAfterThrow(Exception e) //表示操作结果
{
System.out.println("AOP切面操作出现异常:"+e);
}
@Around(value="execution(* com.Spring..*.*(..)))")
public Object serviceAround(ProceedingJoinPoint point) throws Throwable
{
System.out.println("AOP切面数据层方法调用之前,参数:"+Arrays.toString(point.getArgs()));
Member vo=new Member();
vo.setMid("TestAOP");
vo.setName("测试AOP");
Object retVal=point.proceed(new Object[]{ vo });
System.out.println("AOP切面数据层方法调用之后,返回值:"+retVal);
return true;
}
}

运行结果:

可以对照之前用注解的xml配置:

<context:annotation-config/>
<context:component-scan base-package="com.Spring"/>
<aop:aspectj-autoproxy proxy-target-class="true"/> <aop:config>
<!-- 定义程序的切入点 -->
<aop:pointcut expression="execution(* com.Spring..*.*(..)) and args(vo))" id="pointcut"/>
<!-- 这里ref的对象是通过annotation配置@Component出来的, -->
<!-- 定义面向方面的处理类 -->
<aop:aspect ref="serviceAspect">
<!--
<aop:before method="serviceBefore2" pointcut-ref="pointcut" arg-names="vo"/>
<aop:after method="serviceAfter" pointcut="execution(* com.Spring..*.*(..)))"/>
<aop:after-returning method="serviceAfterReturn" pointcut="execution(* com.Spring..*.*(..)))" returning="haha" arg-names="haha"/>
<aop:after-throwing method="serviceAfterThrow" pointcut="execution(* com.Spring..*.*(..)))" arg-names="e" throwing="abc"/>
-->
<aop:around method="serviceAround" pointcut="execution(* com.Spring..*.*(..)))" />
</aop:aspect>
</aop:config>

实际操作中,需要进行一些辅助性功能编写的时候(比如日志记录),建议使用annotation的配置操作,这样的代码是最简化的,也是最直观的。

18-spring学习-利用Annotation配置AOP的更多相关文章

  1. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  2. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  3. Spring学习资料以及配置环境

    一.Spring4 1.介绍 新特性 SpringIDE 插件 IOC DI 在 Spring 中配置 Bean 自动装配 Bean 之间的关系(依赖.继承) Bean 的作用域 使用外部属性文件 S ...

  4. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  5. Spring 学习(三)AOP

    (1)AOP概述 - AOP:面向切面编程,扩展功能不修改源代码实现 - AOP采取横向抽取机制,取代了传统的纵向继承体系重复性代码 (2)AOP底层原理 原始方法------->纵向继承体系 ...

  6. 【JavaEE】SSH+Spring Security基础上配置AOP+log4j

    Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中 ...

  7. Spring学习(八)AOP详解

    文章更新时间:2020/04/06 一.一个例子 在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让 ...

  8. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  9. Spring学习之旅(五)--AOP

    什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...

随机推荐

  1. bzoj 1086 树分块

    将树分成一些块,做法见vfleaking博客. /************************************************************** Problem: 108 ...

  2. JDK源码(1.7) -- java.util.List<E>

    java.util.List<E> 源码分析(JDK1.7) --------------------------------------------------------------- ...

  3. 卡尔曼滤波(Kalman Filter)在目标边框预测中的应用

    1.卡尔曼滤波的导论 卡尔曼滤波器(Kalman Filter),是由匈牙利数学家Rudolf Emil Kalman发明,并以其名字命名.卡尔曼出生于1930年匈牙利首都布达佩斯.1953,1954 ...

  4. 为什么MyISAM会比Innodb的查询速度快

    INNODB在做SELECT的时候,要维护的东西比MYISAM引擎多很多: 1)数据块,INNODB要缓存,MYISAM只缓存索引块,  这中间还有换进换出的减少: 2)innodb寻址要映射到块,再 ...

  5. centos安装gcc

    1.安装gcc基本开发工具环境 yum groupinstall 'Development Tools' 2.安装完成后查看安装是否成功 whereis gcc 3.查看gcc版本 gcc --ver ...

  6. gitignore / Delphi.gitignore

    https://github.com/github/gitignore/blob/master/Delphi.gitignore *.dcu *.~*~ *.local *.identcache __ ...

  7. unsigned int与int相加的问题-----C/C++小知识 区别

    http://blog.csdn.net/thefutureisour/article/details/8147277 #include "stdafx.h" int _tmain ...

  8. CMoReader

    #ifndef __E3GLOGLOADBYFILE_H__ #define __E3GLOGLOADBYFILE_H__ #include "PubCommon\MemoryManager ...

  9. win8升级8.1提示卸载sentinel runtime drivers

    Win8升级8.1时提示需卸载sentinel runtime drivers的解决方法 第一步:打开sentinelcustomer.safenet-inc.com/sentineldownload ...

  10. Java ClassLoader加载机制理解 实际例子

    针对 Java ClassLoader加载机制理解, 做了个如何自定制简单的ClassLoader,并成功加载指定的类. 不废话,直接上代码. package com.chq.study.cl; im ...