1、AOP概念
所说的面向切面编程其实就是在处理一系列业务逻辑的时候这一系列动作看成一个动作集合。比如连接数据库来说:
加载驱动-----获取class--------获取连接对象-------访问数据库------查询---------操作结果
对于上面的这一系列动作我们把其中的虚线看成是一个个的切面。然后我们在虚线的位置上加入一些逻辑。哪怕是日志,这也就成就了在不知不觉中将逻辑处理加入到了相应的位置上。而形成了所谓的面向切面编程!
 
 
下面通过@Before演示Aop织入到方法之前执行一些逻辑
 
[html] view plain copy
 
  1. AOP的应用:
  2. Xml配置  这里的xml配置引入了aop的xsd语法约束。另外加入的aop的自动代理标签。请看注释
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans";
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  6. xmlns:context="http://www.springframework.org/schema/context";
  7. xmlns:aop="http://www.springframework.org/schema/aop";
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"; >
  14. <context:annotation-config/>
  15. <!-- 配置容器资源扫描的包 -->
  16. <context:component-scan base-package="com.spring" />
  17. <!-- 自动产生代理,内部实现是用AspectJ实现。可以使用aspectj的注解产生代理 -->
  18. <aop:aspectj-autoproxy />
  19. </beans>
 
 
 
加入Jar包:
1、编写被注入的方法。必须得是spring容器管理的对象
 
[java] view plain copy
 
  1. import org.springframework.stereotype.Component;
  2. import com.spring.dao.UserDao;
  3. @Component("userService")
  4. public class UserServiceImpl implements UserService{
  5. private UserDao userDao;
  6. public void setUserDao(UserDao userDao) {
  7. this.userDao = userDao;
  8. }
  9. //在下面方法前面加逻辑
  10. public void HelloWorld(){
  11. System.out.println("helloworld");
  12. }
  13. }
2、织入方法的配置:
 
[java] view plain copy
 
  1. package com.spring.aop;
  2. import org.aspectj.lang.annotation.Aspect;
  3. import org.aspectj.lang.annotation.Before;
  4. import org.springframework.stereotype.Component;
  5. //声明切面
  6. @Aspect
  7. //加入Spring管理容器
  8. @Component("LogInterceptor")
  9. public class LogInterceptor {
  10. //指定织入的方法。
  11. @Before("execution(public void com.spring.service.UserServiceImpl.HelloWorld())")
  12. public void BeforeMethod(){
  13. System.out.println("method start!");
  14. }
  15. }
  16. 测试aop的织入:
  17. public class SpringTest {
  18. @Test
  19. public void test01() {
  20. BeanFactory applicationContext = new ClassPathXmlApplicationContext(
  21. "beans.xml");
  22. UserService user = (UserService) applicationContext.getBean("userService");
  23. user.HelloWorld();
  24. }
  25. }
 
 
 
注意:
在编写过程中的织入点语法上指定before织入哪个方法的前面。而括号中的这个被指定的织入点最好是实现一个接口。如果不实现接口的话就会报异常为:

Cannot proxy target class because CGLIB2 is not available.Add CGLIB to the class path or specify proxy interfaces

这个异常的解决办法为:
加入cglib.jar。因为被织入对象的方法单位如果没有实现接口的话它就需要cglib.jar的支持。
 
AOP基本概念:
JoinPoint:切入点、可以理解为上面案例的HelloWorld方法之前就为一个Joinpoint
Pointcut:切入点的集合,可以通过织入点语法定义N个切入点加入逻辑处理。
Aspect:切面,指的是切面的类。也就是上面声明Aspect的逻辑集合
Advise:指的是切面上的逻辑比如@Before、@After
Target:被代理对象.上面的案例指的是UserServiceImpl
Weave:织入

Spring Aop的理解和简单实现的更多相关文章

  1. Spring AOP深入理解之拦截器调用

    Spring AOP深入理解之拦截器调用 Spring AOP代理对象生成回想 上一篇博客中:深入理解Spring AOP之二代理对象生成介绍了Spring代理对象是怎样生成的,当中重点介绍了JDK动 ...

  2. Spring AOP初级——入门及简单应用

      在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...

  3. 基于Spring aop写的一个简单的耗时监控

    前言:毕业后应该有一两年没有好好的更新博客了,回头看看自己这一年,似乎少了太多的沉淀了.让自己做一个爱分享的人,好的知识点拿出来和大家一起分享,一起学习. 背景: 在做项目的时候,大家肯定都遇到对一些 ...

  4. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  5. spring aop 的理解

    spring aop的相关概念(所有的概念都是为了生成代理类这个过程所需要的信息的抽象): 1.Targer:目标对象.被代理的对象. 2.Advice:增强/通知.就是为目标对象扩展的功能.分为前置 ...

  6. 使用Spring AOP 实现日志管理(简单教程)

    有时候,我们在做项目时会遇到这样的需求: 给XXX.java中的所有方法加上指定格式的日志输出. 针对这种指定类.或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现. 本文使用 ...

  7. Spring AOP的理解和使用

    AOP是Spring框架面向切面的编程思想,AOP采用一种称为“横切”的技术,将涉及多业务流程的通用功能抽取并单独封装,形成独立的切面,在合适的时机将这些切面横向切入到业务流程指定的位置中. 掌握AO ...

  8. Spring AOP概念理解

    1.我所知道的aop 初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后 ...

  9. Spring AOP的理解(通俗易懂)。

    转载 原文链接:http://www.verydemo.com/demo_c143_i20837.html 这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. 1. ...

随机推荐

  1. jquery中的ajax方法参数的用法和他的含义:

    转自:https://www.cnblogs.com/huiyuantang/p/5458278.html 1.url:  要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.ty ...

  2. 完美解决onchange不能实时的监听

    我们大家都知道onchange有时候很不好用,因为onchange事件是离开焦点后才会被触发,而不是实时去监听! 那么oninput()事件和onpropertychange()完美的解决了问题:(o ...

  3. webstorm添加调试nodejs

    打开run菜单选择Edit Configurations 展开defaults菜单,选择nodejs 点击+按钮,选择Node.js,出现下面弹出框. 点击ok保存

  4. CentOS7安装Nmon(linux性能监控工具)

    Nmon开源性能监控工具,用于监控linux系统的资源消耗信息,并能把结果输出到文件中,然后通过nmon_analyser工具产生数据文件与图形化结果. 目录 一.安装软件二.实时监控三.数据采集四. ...

  5. beginAppearanceTransition

    - (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated __OSX_AVAILABLE_STARTING ...

  6. 006-jdk1.5版本新特性

    一.Java SE 5.0 (1.5.0) 名称:Tiger(老虎) 发布日期:2004-09-30 新特性: 1.1.静态导入 定义:静态导入用于简化程序对类静态属性和方法的调用. 语法: impo ...

  7. [PAT]A+B Format[简单]

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  8. kvm日常管理

    创建虚拟机 快速启动虚拟机 [root@localhost ~]# yum install kvm libvirt python-virtinst qemu-kvm virt-viewer bridg ...

  9. C#调用VP 包含素材

    VS2012 +VP9.0 ***************** 自己运行的时只要修改VP里面素材的路径即可 链接: https://pan.baidu.com/s/1J6Bc5FcBYLZLgqe30 ...

  10. pythonon ddt数据驱动二(json, yaml 驱动)

    这一篇主要是关于文件的数据驱动. 一.通过json文件驱动 @ddt class MyTest(unittest.TestCase): @file_data('test_data_list.json' ...