在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. 查看 php 编译参数

    /app/php/bin/php -i|grep configure 范例 4: [root@VM-001 ~]# /app/php/bin/php -i|grep configure Configu ...

  2. Linux-定时任务排查

    前段时间,哥们的服务器被人反弹了shell,由于反弹的地址不可达,系统总是会发送一条mail邮件到root账户,导致入侵行为被发现,由于反弹的动作是通过crontab来定时执行的,所以来梳理下cron ...

  3. 解决ssh登录很慢的问题以及jumpserver登录主机出现:Authentication timeout

    一.问题 登录ssh输入用户名后,等待很长时间20-30秒左右才显示输入密码提示框,从而导致通过jumpserver堡垒机登录主机时,报错:Authentication timeout Opt> ...

  4. Django学习系列2:django环境中安装selenium并查看selenium版本号

    在Django环境中安装selenium (django) root@ranxf-TEST:/studydisk/Python_web_TDD/superlists# conda install se ...

  5. RabbitMQ之消息持久化(队列持久化、消息持久化)

    rabbitMQ不支持数据库的持久化,只支持内存以及文件持久化 https://blog.csdn.net/bwh0520/article/details/78746873 http://blog.y ...

  6. linux 最大文件打开数

    配置文件 vim /etc/security/limits.conf   # /etc/security/limits.conf##This file sets the resource limits ...

  7. hash_table

    #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using name ...

  8. Maven工程构建

    Maven 相关术语: 本地资源库:用来存储项目的依赖库,默认的文件夹是 “.m2” 目录 中央存储库: 用来下载所有项目的依赖库的默认位置 Maven pom.xml: 项目对象模型(Project ...

  9. vue-cli3.0的配置

    转自 https://www.cnblogs.com/sangzs/p/9543242.html module.exports = { // 基本路径 baseUrl: '/', // 输出文件目录 ...

  10. SecureRandom的坑

    之前写随机数的时候一直用SecureRandom.getInstanceStrong()方法生成SecureRandom实例,进而调用其各种next方法.突然有一次,发现next方法卡住了, 每一次调 ...