在spring中有两种增强方式:XML配置文件和注解配置。下面一次为大家讲解。

  使用的是Aspectj第三方框架 纯POJO (在XML中配置节点)

   

  使用@AspectJ,首先要保证所用的JDK 是5.0或以上版本

  1)首先,创建一个切入点MyAspect,代码如下:

 public class MyAspect {
// 前置通知
public void myBefore() {
System.out.println("这是前置增强");
}
//前置通知带参
public void before(JoinPoint jp) {
System.out.println("前置通知方法before() jp = " + jp);
} // 后置通知
public void myAfterReturning() {
System.out.println("这是后置增强");
} // 后置通知带参
public void afterReturing(String result) {
System.out.println("后置通知方法 result = " + result);
} // 环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕通知方法,目标方法执行之前");
// 执行目标方法
Object result = pjp.proceed();
System.out.println("环绕通知方法,目标方法执行之后");
return ((String) result).toUpperCase();
} // 异常通知
public void afterThrowing() {
System.out.println("异常通知方法");
} public void afterThrowing(Exception ex) {
System.out.println("异常通知方法 ex = " + ex.getMessage());
} // 最终通知
public void after() {
System.out.println("最终通知方法");
}
}

通知(MyAspect.java)

  2)applicationContext.xml中配置如下

 <?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
//需要注意的是这里必须要引用aop命名空间,否则一切都是空谈~
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 目标对象 -->
<bean id="someService" class="entity.SomeServiceImpl"></bean> <!-- 切面: -->
<bean id="myAspect" class="aop.MyAspect"></bean> <aop:config>
<!--expression:切入点表达式 -->
<aop:pointcut expression="execution(public * *..ISomeService.doLog(..))"
id="beforePointcut" />
<aop:aspect ref="myAspect"><!-- ref:指定切面 -->
<!-- method:指定切面类中的方法; pointcut-ref:指定定义的切点 -->
<!-- 前置增强 -->
<aop:before method="myBefore" pointcut-ref="beforePointcut" />
<!-- 前置增强 带参 -->
<aop:before method="before(org.aspectj.lang.JoinPoint)"
pointcut-ref="beforePointcut" />
<!-- 后置增强 -->
<aop:after-returning method="myAfterReturning"
pointcut-ref="beforePointcut" />
<!-- 后置增强 带参 -->
<aop:after-returning method="afterReturing(java.lang.String)"
pointcut-ref="beforePointcut" returning="result" />
<!-- 环绕增强 -->
<aop:around method="around" pointcut-ref="beforePointcut" />
<!-- 异常增强 -->
<aop:after-throwing method="afterThrowing"
pointcut-ref="beforePointcut" />
<!-- 异常增强 带参 -->
<aop:after-throwing method="afterThrowing(java.lang.Exception)"
pointcut-ref="beforePointcut" throwing="ex" />
<!-- 最终增强 -->
<aop:after method="after" pointcut-ref="beforePointcut" />
</aop:aspect>
</aop:config>
</beans>

applicationContext.xml配置文件

  3)运行结果如下:

  

     2.使用注解定义增强

  AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供代码的织入

@AspectJ是AspectJ 5新增的功能,使用JDK 5.0 注解技术和正规的AspectJ切点表达式语言描述切面

Spring通过集成AspectJ实现了以注解的方式定义增强类,避开了在xml中的节点操作,大大减少了配置文件中的工作量

  ps:在一个项目中,既有注解还有xml配置文件,那么注解优于xml配置文件而优先执行。

  

  1)在切面中配置注解AOP增强

 package aop;
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; //标示该类为切面(切入点表达式在这里不做讲解)
@Aspect
public class MyAspect { // 前置通知
@Before(value = "execution(public * *..ISomeService.doLog(..))")
public void myBefore() {
System.out.println("这是前置增强");
} // 后置通知
@AfterReturning(value = "execution(public * *..ISomeService.doLog(..))")
public void myAfterReturning() {
System.out.println("这是后置增强");
} // 环绕增强
@Around(value = "execution(public * *..ISomeService.doLog(..))")
public void myAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕前置增强"); pjp.proceed(); System.out.println("这是环绕后置增强");
} // 异常增强
@AfterThrowing(value = "execution(public * *..ISomeService.doLog(..))")
public void myAfterThrowing() {
System.out.println("这是异常增强");
} // 最终增强
@After(value = "execution(public * *..ISomeService.doLog(..))")
public void myAfter() {
System.out.println("这是最终增强");
}
}

MyAspect.java

  2)在applicationContext.xml中配置一个自动代理节点

 <?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="someService" class="entity.SomeServiceImpl"></bean> <!-- 切面: -->
<bean id="myAspect" class="aop.MyAspect"></bean> <!-- 自动代理:弄个标签就可以自动代理了,比之前繁琐的步骤简单多了 -->
<aop:aspectj-autoproxy/>
</beans>

applicationContext.xml配置文件

  3)输出结果

  

   MyTest.java(上面的xml配置文件和注解都可以使用,只要对应目标对象的id/name就可以了)

public class MyTest {
@Test
public void Test1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ISomeService biz=
//对应目标对象的id或者name属性
(ISomeService)ctx.getBean("someService");
biz.doLog();
biz.doTransaction();
System.out.println("success!");
}
}

测试类

spring中的增强类型的更多相关文章

  1. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  2. spring---aop(9)---Spring AOP中引入增强

    写在前面 Spring将introduction通知看作一种特殊类型的拦截通知.用Spring的行话来讲,对方法的增强叫做Wearing(织入),而对类的增强叫introduction(引入).Int ...

  3. Spring支持5种类型的增强

    Spring支持5种类型的增强:1.前置增强:org.springframework.aop.BeforeAdvice代表前置增强,因为Spring只支持方法级的增强,所以MethodBeforeAd ...

  4. 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException

    解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...

  5. spring中Bean的注入类型

    1.属性注入    即通过setXxx()方法注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入是实际应用中最常采用的注入方式.    属性注入要求Bean提供 ...

  6. Spring中类型自动装配--byType

    在Spring中,“类型自动装配”的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它. 例如,一个“persion” bean 公开以“ability”类数据类型作为 ...

  7. 使用IDEA详解Spring中依赖注入的类型(上)

    使用IDEA详解Spring中依赖注入的类型(上) 在Spring中实现IoC容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态地将其所依赖的对象(例如属性值)注入Bean组件 ...

  8. 【Java EE 学习 50】【Spring学习第二天】【使用注解的DI实现】【spring中的继承】【动态代理伪hibernate实现】

    一.使用注解的DI实现 1.@Resource 使用该注解能够实现引用型属性的DI实现,该注解能够根据属性名和属性类型自动给属性赋值.一般使用@Resource(name="student& ...

  9. Spring4.1新特性——Spring缓存框架增强(转)

    目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ...

随机推荐

  1. 【TensorFlow探索之一】MNIST的初步尝试

    最近在学习TensorFlow,尝试的第一个项目是MNIST.首先给出源码地址. 1 数据集的获取 我们可以直接运行下面的代码,来获取到MNIST的数据集. from tensorflow.examp ...

  2. Python 输出百分比

    注:python3环境试验 0x00 使用参数格式化{:2%} {:.2%}: 显示小数点后2位 print('{:.2%}'.format(10/50)) #percent: 20.00% {:.0 ...

  3. 批量修改zencart产品价格、原价、特价、产品属性价格

    批量修改zencart商品价格无非只有下面几种情况: 一 在原来基础上批量调高一定比例 二 将原来的价格批量换成一个新的价格 针对第一种情况的话,网上很多人已经给出了解决办法: 利用SQL语句批量修改 ...

  4. JVM体系结构及优化

    源文档:https://docs.oracle.com/javase/8/docs/technotes/guides/vm/index.html JVM体系结构     方法区,类加载器,堆,Java ...

  5. vue 无缝滚动文字

    前言 用vue做无缝滚动,字体弹幕 就上代码吧 <head> <meta charset="UTF-8"> <style> div, ul, l ...

  6. tar命令--数据归档(二)

    tar -cf all.tar *.jpg 这条命令是将所有.jpg的文件打成一个名为all.tar的包.-c是表示产生新的包,-f指定包的文件名. tar -rf all.tar *.gif 这条命 ...

  7. Eclipse修改背景颜色(豆沙绿)

    操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以我经常会改变workspace的背景色,使眼睛舒服一些.设置方法如下: 1.打开window->Prefere ...

  8. CF1257E/F

    E 给出三个序列共n个元素,每个元素值为1~n且不重 一次可以把一个元素换到另一个序列中,求最少操作次数使得三个序列(可为空)分别排序后并在一起为1~n顺序 题解 (伪)神仙题 随便dp,依次考虑每个 ...

  9. 3828. 三角形计数 3829. ZCC loves Isaac 3830. 字符消除

    3828 给定n个点的坐标(0<=xi,yi<=10000)求选出任意三个点能组成的三角形的总面积. 题解 太naive了 枚举三角形的y最小的点,把剩余的点按角度排序 然后随便算,可以用 ...

  10. DevOps之持续集成Pipeline(一)

    一.Pipeline介绍     Jenkins2.0中最大的一个特性就是Pipeline,实际使用中Pipeline已经超越了我们对jenkins本身的理解,可能在之前我们大多数把Jenkins当做 ...