在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. java gRPC四种服务类型简单示例

    一.gRPC 简介 gRPC 是Go实现的:一个高性能,开源,将移动和HTTP/2放在首位通用的RPC框架.使用gRPC可以在客户端调用不同机器上的服务端的方法,而客户端和服务端的开发语言和 运行环境 ...

  2. 60. Permutation Sequence (JAVA)

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  3. 使用Python的pandas-datareader包下载雅虎财经股价数据

    0 准备工作 首先,使用pip方法安装pandas和pandas-datareader两个功能包. 安装的方法十分简单,以管理员身份运行cmd. 输入以下命令. $ pip install panda ...

  4. 电脑系统win7和samba服务器连接不上解决办法

    1.修改本地安全策略运行secpol.msc打开“本地安全策略”窗体,依次点开“本地策略”->“安全选项”,修改“网络安全: LAN 管理器身份验证级别”的值为“发送 LM 和 NTLM – 如 ...

  5. UILabel的行间距,字间距处理

    啥都不说了,直接上代码,做了一个Category #import <UIKit/UIKit.h> @interface UILabel (ChangeLineSpaceAndWordSpa ...

  6. 【java】并发执行ExecutorService的sumbit返回值的顺序问题

    ArrayList<Future> fl = new ArrayList<Future>(); for (int i = 0; i < 10; i++) { Future ...

  7. 【crontab】误删crontab及其恢复

    中秋节快到了,首先祝自己中秋快乐. 昨天下午六点,心里正想着加完一个crontab就可以下班了.本来想执行 crontab -e的,没想到手一抖就输入了crontab ,然后就进入了下面这个样子.

  8. time时间库使用示例

    time时间库主要有以下几个方法 1. 生成struct_time ,然后就可以很方便的获取到年月日,时分秒等信息 time.localtime() 2. 生成时间戳 time.time() 3. 将 ...

  9. C# WPF开机自启动和只允许一个程序运行

    本文出自:https://www.cnblogs.com/2186009311CFF/p/10024949.html 在App.xaml.cs填充一下内容,即可实现只允许一个运行,且不解锁屏幕的情况下 ...

  10. 代码检测docker-sonarqube

    gitlab-ce + gitlab-runner + sonarqube,在提交代码时对代码质量进行检测,对不符合要求的代码不允许提交到gitlab version: '3.1' services: ...