Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点。简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后添加额外的功能。
在Spring AOP中,有 4 种类型通知(advices)的支持:
  • 通知(Advice)之前 - 该方法执行前运行
  • 通知(Advice)返回之后 – 运行后,该方法返回一个结果
  • 通知(Advice)抛出之后 – 运行方法抛出异常后,
  • 环绕通知 – 环绕方法执行运行,结合以上这三个通知。
下面的例子显示Spring AOP 通知如何工作。

接口

  1. public interface UserService {
  2. public void addUser();
  3. public String eat();
  4. }

实现类

  1. public class UserServiceImpl implements UserService {
  2. public void addUser() {
  3. System.out.println("添加用户");
  4. }
  5.  
  6. public String eat() {
  7. String some="apple";
  8. System.out.println("eat a little apple");
  9. return some;
  10. }
  11. }

前置增强

  1. public class BeforeAdvice implements MethodBeforeAdvice {
  2. /**
  3. *
  4. * @param method 目标方法
  5. * @param objects 目标方法的参数列表
  6. * @param o 目标对象
  7. * @throws Throwable
  8. */
  9. public void before(Method method, Object[] objects, Object o) throws Throwable {
  10. System.out.println("前置增强---------------");
  11. }
  12. }

后置增强

  1. public class AfterAdvice implements AfterReturningAdvice {
  2. public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
  3. System.out.println("后置增强");
  4. }
  5. }

环绕增强     (出现在前置增强之后 ,后置增强之前)

  1. public class AroundAdvice implements MethodInterceptor {
  2. /**
  3. *
  4. * @param methodInvocation
  5. * @return
  6. * @throws Throwable
  7. */
  8. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  9. System.out.println("执行方法之前的环绕 通知");
  10. Object result = methodInvocation.proceed();//方法的执行 主业务的执行
  11. if(result!=null){
  12. result="小黑";
  13. }
  14. System.out.println("执行方法之后的环绕 通知");
  15. return result;
  16. }
  17. }

ApplicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  8. <!--目标对象-->
  9. <bean id ="userservice" class="cn.kitty.service.UserServiceImpl"></bean>
  10. <!--配置前置通知-->
  11. <bean id="beforeAdvice" class="cn.kitty.service.BeforeAdvice"></bean>
  12. <!--配置后置通知-->
  13. <bean id="afterAdvice" class="cn.kitty.service.AfterAdvice"></bean>
  14. <!--配置环绕通知-->
  15. <bean id="aroundAdrice" class="cn.kitty.service.AroundAdvice"></bean>
  16.  
  17. <!-- 配置代理工厂bean 生成代理类 把通知织入到目标对象-->
  18. <bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  19. <!--注册目标对象-->
  20. <property name="targetName" value="userservice"></property>
  21. <!--注册通知-->
  22. <property name="interceptorNames" >
  23. <array>
  24. <value>beforeAdvice</value>
  25. <value>afterAdvice</value>
  26. <value>aroundAdrice</value>
  27. </array>
  28. </property>
  29. </bean>
  30. </beans>

test 类

  1. public class Test1011 {
  2. @Test
  3. public void yichang(){
  4. ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  5. UserService service= (UserService) context.getBean("userProxy");
  6. service.addUser();
  7. System.out.println("---------------");
  8. service.eat();
  9. }
  10. @Test
  11. public void around(){
  12. ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  13. UserService service= (UserService) context.getBean("userProxy");
  14. service.addUser();
  15. System.out.println("---------------");
  16. service.eat();
  17. }
  18. @Test
  19. public void before(){
  20. ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  21. UserService service= (UserService) context.getBean("userProxy");
  22. service.addUser();
  23. System.out.println("---------------");
  24. service.eat();
  25. }
  26. @Test
  27. public void after(){
  28. ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  29. UserService service= (UserService) context.getBean("userProxy");
  30. service.addUser();
  31. System.out.println("---------------");
  32. service.eat();
  33. }
  34.  
  35. }

Spring Advice的更多相关文章

  1. 关于面试别问及Spring如何回答思路总结!

    首先要知道 Spring两大核心IOC和AOP(Java轻量级业务层框架Spring两大核心IOC和AOP原理) IOC: 1.从Java最基本的创建对象开始 如Interface Driven De ...

  2. Spring AOP 四大通知

    Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现  MethodBeforeAdvice 接口,重写 public  void  before(Method  metho ...

  3. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  4. spring cuowu

    spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...

  5. Spring 使用javaconfig配置aop

    1.在xml中需要配置自动代理 /** * */ package com.junge.demo.spring.dao; import org.springframework.context.annot ...

  6. Spring 使用xml配置aop

    1.xml文件需要引入aop命名空间 2.xml内容: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  7. spring入门常见的问题及解决办法

    在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...

  8. Spring知识总结

    一.Spring简述    Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架,Spring致力于提供一种方法管理你的业务对象,Spring的主要目的是使JavaE ...

  9. spring错误汇总

    在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结.希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...

随机推荐

  1. asxios--form data提交,setcookie

    React native 项目,部分接口用form data 提交,以及在Android端,虽然设置了请求携带cookie,但每次请求携带的cookie跟初始化时都不一样,目前做法是去到初始化中返回的 ...

  2. 【4】axios 获取数据

    API:https://www.kancloud.cn/yunye/axios/234845 基于axios进行二次封装 安装axios npm install axios --save 安装成功 [ ...

  3. golang fmt格式“占位符”

    # 定义示例类型和变量 type Human struct { Name string } var people = Human{Name:"zhangsan"} 普通占位符 占位 ...

  4. 根据构建类型动态设置AndroidManifest.xml文件中的meta-data

    当debug和release版本使用不同的值时,使用Gradle设置相应的值. Android主配置文件 <meta-data android:name="com.amap.api.v ...

  5. Gradle全局变量定义及引用

    在Project的build.gradle脚本中定义一些全局变量 ext { compileSdkVersion = 21 buildToolsVersion = "24.0.1" ...

  6. Python itsdangerous 生成token和验证token

    代码如下 class AuthToken(object): # 用于处理token信息流程: # 1.更加给定的用户信息生成token # 2.保存生成的token,以便于后面验证 # 3.对用户请求 ...

  7. vue的图片路径,和背景图片路径打包后错误解决

    最近在研究vue,老实的按照官网提供的,搭建的了 webpack+vue+vuex+vue-router,,因为是自己搭建的,所以踩了不少坑,一般问题百度都有,这个背景图片的问题,查了很久才解决. 1 ...

  8. Kotlin 范型约束

    官方的示意及其简约,该说的一概没说 我在这里给大家一个完整的例子 //test.kt fun <T> cloneWhenGreater(list: List<T>, thres ...

  9. come on! linux install JDK9.0.1

    哈哈,JDK9已经出来了 我们把它安装到Linux上吧! 一下载JDK9 http://www.oracle.com/technetwork/java/javase/downloads/index.h ...

  10. java随机排座位

    //打乱学生顺序 Collections.shuffle(); 容我记个单词 peer: vi.凝视; 盯着看; 隐退,若隐若现; 同等,比得上;n.同辈,同等的人; 贵族; 同伴,伙伴;adj.贵族 ...