2019独角兽企业重金招聘Python工程师标准>>>

aop介绍

 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。
  面向切面是面向对象中的一种方式而已。在代码执行过程中,动态嵌入其他代码,叫做面向切面编程。常见的使用场景:
i :日志
ii: 事务
iii:数据库操作

aop的实现方式(schema)

前置通知(MethodBeforeAdvice)

@Component
public class MyBeforeAdvice implements MethodBeforeAdvice{@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println("前置通知执行:"+method.getName());}
}

后置通知(AfterReturningAdvice)

@Component
public class AfterAdvice implements AfterReturningAdvice{@Overridepublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("后置通知 :"+returnValue);}

环绕通知(MethodInterceptor)

@Component
public class AroundInterceptor implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("环绕通知执行1");String msg  = (String)invocation.proceed();System.out.println("环绕通知执行2");return msg==null?null:msg.toUpperCase();}
}

异常处理通知(ThrowsAdvice)

@Component
public class MyThrowsAdvice implements ThrowsAdvice {public void afterThrowing(Exception ex){System.out.println("异常参数了:"+ex.getMessage());}}

测试类

public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");IWindow window =  ac.getBean("mytarget",IWindow.class);window.say();System.out.println("----------");System.out.println(window.doSome());}
}   

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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 开启扫描 --><context:component-scan base-package="com.sxt.*"></context:component-scan><!-- 1.注册目标对象 --><!--        <bean class="com.sxt.pojo.Window" id="window"/>2.注册前置通知对象--><bean class="com.sxt.advice.MyBeforeAdvice" id="methodBeforeAdvice"/> <bean class="com.sxt.advice.AfterAdvice" id="afterAdvice"></bean><bean class="com.sxt.advice.AroundInterceptor" id="around"></bean><bean class="com.sxt.advice.MyThrowsAdvice" id="myThrowsAdvice"></bean> <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="mytarget"><property name="target" ref="window" /><property name="interfaces" value="com.sxt.pojo.IWindow" /><property name="interceptorNames"><array><!-- 关联前置通知 --><value>methodBeforeAdvice</value><value>afterAdvice</value><value>aroundInterceptor</value><value>myThrowsAdvice</value></array></property></bean></beans>

基于aspectJ方式实现

  对于AOP这种编程思想,很多框架都进行了实现。Spring就是其中之一,可以完成面向切面编程。然而,AspectJ也实现了AOP的功能,且其实现方式更为简捷,使用更为方便,而且还支持注解式开发。所以,Spring又将AspectJ的对于AOP的实现也引入到了自己的框架中。在Spring中使用AOP开发时,一般使用AspectJ的实现方式

xml配置的方式

实体类

public class Window implements IWindow{@Overridepublic void say(){System.out.println("div  dir");}@Overridepublic String doSome(){System.out.println("你好");return "hello";}
}

切面类

public class  Advice{public void before(){System.out.println("前置通知");}public void afterReturning(){System.out.println("后置通知");}public Object around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("环绕通知1");Object msg = pjp.proceed();if (msg != null) {msg = msg.toString().toUpperCase();System.out.println("环绕通知2");return msg;}return null;}public void throwing(Exception ex){System.out.println("参数异常了:"+ex.getMessage());}public void after(){System.out.println("最终通知");}
}

测试类


public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");IWindow window =  ac.getBean(IWindow.class);window.say();System.out.println("----------");System.out.println(window.doSome());}
}   

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:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><!-- 注册目标对象 --><bean class="com.sxt.pojo.Window" id="window"></bean><!-- 注册切面对象 --><bean class="com.sxt.advice.Advice" id="myAdvice"></bean><!-- 3.配置AOP --><aop:config><!-- 配置切面对象 --><aop:aspect ref="myAdvice"><!-- 配置切面点 --><aop:pointcut expression="execution(* com.sxt.pojo.*.*(..))" id="pointcut"/><aop:after-returning method="afterReturning" pointcut-ref="pointcut"/> <aop:before method="before" pointcut-ref="pointcut"/>   <aop:around method="around" pointcut-ref="pointcut"/>   <aop:after-throwing method="throwing" pointcut-ref="pointcut" throwing="ex"/>    <aop:after method="after" pointcut-ref="pointcut"/> </aop:aspect></aop:config></beans>

注解的方式

切面类


@Component
@Aspect
public class  Advice{@Before(value="execution(* com.sxt.*.*.*(..))")    public void before(){System.out.println("前置通知");}@AfterReturning(value="execution(* com.sxt.*.*.*(..))")  public void afterReturning(){System.out.println("后置通知");}@Around(value="execution(* com.sxt.pojo.*.*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("环绕通知1");Object msg = pjp.proceed();if (msg != null) {msg = msg.toString().toUpperCase();System.out.println("环绕通知2");return msg;}return null;}@AfterThrowing(value="execution(* com.sxt.pojo.*.*(..))",throwing="ex")public void throwing(Exception ex){System.out.println("参数异常了:"+ex.getMessage());}@After(value="execution(* com.sxt.pojo.*.*(..))")public void after(){System.out.println("最终通知");}
}

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:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><!-- 开启扫描 --><context:component-scan base-package="com.sxt.*"></context:component-scan><!-- 开启aspectJ注解 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

控制台输入

转载于:https://my.oschina.net/u/4116654/blog/3038851

Spring-AOP的实现方法相关推荐

  1. Spring AOP根据JdbcTemplate方法名动态设置数据源

    2019独角兽企业重金招聘Python工程师标准>>> 说明:现在的场景是,采用数据库(Mysql)复制(binlog)的方式在两台不同服务器部署并配置主从(Master-Slave ...

  2. spring AOP对父类方法加强分析

    spring AOP可以对方法进行加强,就是在方法前执行一些想要的事情,执行方法后想执行一些信息,原理就是利用动态代理,具体不在阐述 今天要讨论的是一个springBean继承了父类,在父类里进行了方 ...

  3. 基于 Annotation 拦截的 Spring AOP 权限验证方法

    余 清, 软件工程师, IBM 简介: 使用 Annotation 可以非常方便的根据用户的不同角色,分配访问 Java 方法的权限.在 Java Web 开发中,使用这种方法,可以提高系统的松耦合度 ...

  4. spring AOP实现——xml方法

    上一文中 讲了Annotation如何配置AOP,地址如下:http://5148737.blog.51cto.com/5138737/1428048 使用同样的bean,用xml来实现一下: Hel ...

  5. spring aop 拦截业务方法,实现权限控制

    难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...

  6. spring aop不执行_使用Spring AOP重试方法执行

    spring aop不执行 我的一位博客关注者发送了一封电子邮件,要求我显示" Spring AOP的RealWorld用法"示例. 他提到,在大多数示例中,都演示了Spring ...

  7. 使用Spring AOP重试方法执行

    我的一位博客关注者发送了一封电子邮件,要求我显示" Spring AOP的RealWorld用法"示例. 他提到,在大多数示例中,都演示了Spring AOP在日志记录方法进入/退 ...

  8. Spring AOP方法分析

    Spring AOP方法分析 此示例显示如何配置Spring AOP方法概要分析.我们可以在任何服务(或其他)类中使用Spring AOP和任何方法,而无需在任何服务类中编写任何一行分析代码.面向方面 ...

  9. 一个Spring AOP的坑!很多人都犯过!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 很多读者看完之后表示用起来很爽,但是后台也有人留言说自己配 ...

  10. 比较Spring AOP与AspectJ

    本文翻译自博客Comparing Spring AOP and AspectJ 介绍 如今有多个可用的AOP库,这些组件需要回答一系列的问题: 是否与我现有的应用兼容? 我在哪实现AOP? 集成到我的 ...

最新文章

  1. 电脑html按键侧滑广告,HTML5侧滑聊天面板
  2. pytorch volatile 和 requires_grad
  3. java网络编程(二)
  4. 重物码垛搬运机器人_搬运码垛机器人的特点及应用
  5. Qt Creator创建Qt Quick项目
  6. python中mainloop什么意思_很难理解python中的Tkinter mainloop()
  7. Jacobi并行拆解
  8. 浙企加入中国大数据产业生态联盟 共商数据价值
  9. 黑龙江省2021高考成绩排名查询,黑龙江高考排名对应学校-高考位次对应大学(2021年理科参考)...
  10. 软件测试--环境讲解
  11. codevs1380 没有上司的舞会
  12. python简易消息连续发送代码
  13. C# 使用iTextSharp中间件打印PDF
  14. Apollo详解之canbus模块——综述
  15. 可否推荐一个香港主机?
  16. python function gamma_Python math gamma()用法及代码示例
  17. windows下qt android开发
  18. 美国国土安全部:Log4j 漏洞的影响将持续十年或更久
  19. 归并排序(默认2路归并)
  20. 提高制作PPT效率,让这些资源来帮你

热门文章

  1. 程序员必备!CSDN 公众号新功能上线!现在体验有惊喜!
  2. 程序员的日常,过于真实 | 每日趣闻
  3. WPF中获取鼠标相对于桌面位置
  4. 打包应用和构建Docker镜像(docker在windows上)
  5. k8s 重要概念 - 每天5分钟玩转 Docker 容器技术(117)
  6. java-结合c3p0封装的db 事务 类
  7. 实现多条件模糊查询SQL语句
  8. Info:Memory module [DIMM] needs attention: Single-bit warning error rate exceeded, Single-bit fai...
  9. linux中生成考核用的GPT分区表结构修复案例
  10. 【收藏】银联在线支付商户UPMP接口的使用和说明