Spring底层的代理的实现:


不带切点的切面是对类里面的所有的方法都进行拦截.


做Spring AOP的开发需要两个包:一个是AOP的包,一个是AOP联盟的包(因为规范是由AOP联盟提出来的).



用Spring开发不用自己手写代码生成代理.Spring是可以通过配置生成代理的.


ProxyFactoryBean会帮你自动生成代理.

通过配置就可以让Spring自动生成代理对象了.这就是不带切点的切面.但是这种方式以后几乎不会用,因为你有一个类就得配置一段,你要是有一百个类就得配置一百次下面这段代码

  1. <!-- 手动生成代理太麻烦了.Spring支持配置生成代理: -->
  2. <!-- 代理对象 -->
  3. <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  4. <!-- 设置目标对象 -->
  5. <property name="target" ref="customerDao"></property>
  6. <!-- 设置实现的接口,value中写接口的全路径 -->
  7. <!-- 类实现的接口的全路径 -->
  8. <property name="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"></property>
  9. <!-- 需要使用value:要的名称 使用beforeAdvice对它进行增强 -->
  10. <property name="interceptorNames" value="beforeAdvice"></property><!--interceptorNames要拦截的名称 -->
  11. <!-- 这个是针对类的所有方法进行拦截的,你还没到配置方法的那一步呢 -->
  12. </bean>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 引入beans的头 -->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  7. <!-- 定义目标对象 -->
  8. <bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl">
  9.  
  10. </bean>
  11. <!-- 定义增强 增强对象-->
  12. <bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice">
  13. </bean>
  14.  
  15. <!-- 手动生成代理太麻烦了.Spring支持配置生成代理: -->
  16. <!-- 代理对象 -->
  17. <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  18. <!-- 设置目标对象 -->
  19. <property name="target" ref="customerDao"></property>
  20. <!-- 设置实现的接口,value中写接口的全路径 -->
  21. <!-- 类实现的接口的全路径 -->
  22. <property name="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"></property>
  23. <!-- 需要使用value:要的名称 使用beforeAdvice对它进行增强 -->
  24. <property name="interceptorNames" value="beforeAdvice"></property><!--interceptorNames要拦截的名称 -->
  25. <!-- 这个是针对类的所有方法进行拦截的,你还没到配置方法的那一步呢 -->
  26. </bean>
  27. </beans>
  1. package cn.itcast.spring3.demo3;
  2.  
  3. public interface CustomerDao {
  4. public void add();
  5. public void delete();
  6. public void update();
  7. public void find();
  8. }
  1. package cn.itcast.spring3.demo3;
  2.  
  3. public class CustomerDaoImpl implements CustomerDao {
  4.  
  5. public void add() {
  6. // TODO Auto-generated method stub
  7. System.out.println("添加客户");
  8. }
  9.  
  10. public void delete() {
  11. // TODO Auto-generated method stub
  12. System.out.println("删除客户");
  13. }
  14.  
  15. public void update() {
  16. // TODO Auto-generated method stub
  17. System.out.println("修改客户");
  18. }
  19.  
  20. public void find() {
  21. // TODO Auto-generated method stub
  22. System.out.println("查询客户");
  23. }
  24.  
  25. }
  1. package cn.itcast.spring3.demo3;
  2.  
  3. import java.lang.reflect.Method;
  4.  
  5. import org.springframework.aop.MethodBeforeAdvice;
  6.  
  7. /**
  8. * 前置增强
  9. * @author zhongzh
  10. *
  11. */
  12. public class MyBeforeAdvice implements MethodBeforeAdvice{
  13. /*
  14. * method:执行的方法
  15. * args:参数
  16. * target:目标对象
  17. * 要对目标对象的某一方法增强
  18. * (non-Javadoc)
  19. * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
  20. */
  21. public void before(Method method, Object[] args, Object target)
  22. throws Throwable {
  23. // TODO Auto-generated method stub
  24. System.out.println("前置增强.....");
  25. }
  26.  
  27. }
  1. package cn.itcast.spring3.demo3;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. @ContextConfiguration("classpath:applicationContext.xml")
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. public class SpringTest3 {
  12. @Autowired
  13. //@Qualifier("customerDao")//和applicationContext.xml中配置的id=customerDao对应 注入的是真实的对象,必须注入代理对象.
  14. @Qualifier("customerDaoProxy")
  15. private CustomerDao customerDao;
  16.  
  17. @Test
  18. public void demo1(){
  19. customerDao.add();
  20. customerDao.delete();
  21. customerDao.update();
  22. customerDao.find();
  23.  
  24. }
  25.  
  26. }

day39-Spring 05-Spring的AOP:不带有切点的切面的更多相关文章

  1. day39-Spring 06-Spring的AOP:带有切点的切面

    环绕增强功能是最强的,它相当于前置增强和后置增强. 这就是带有切点的切面 package cn.itcast.spring3.demo4; import org.aopalliance.interce ...

  2. Spring的IOC和AOP之深剖

    今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...

  3. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  4. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...

  5. Spring总结六:AOP(面向切面编程)

    概述: AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是一种新的 ...

  6. Spring AOP(通知、连接点、切点、切面)

    一.AOP术语 通知(Advice)  切面的工作被称为通知.通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题.5种通知类型: 前置通知(Before): ...

  7. Spring MVC 中使用AOP 进行统一日志管理--注解实现

    1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...

  8. 【Spring】每个程序员都使用Spring(四)——Aop+自定义注解做日志拦截

    一.前言 上一篇博客向大家介绍了Aop的概念,对切面=切点+通知 .连接点.织入.目标对象.代理(jdk动态代理和CGLIB代理)有所了解了.理论很强,实用就在这篇博客介绍. 这篇博客中,小编向大家介 ...

  9. 这一次搞懂Spring代理创建及AOP链式调用过程

    文章目录 前言 正文 基本概念 代理对象的创建 小结 AOP链式调用 AOP扩展知识 一.自定义全局拦截器Interceptor 二.循环依赖三级缓存存在的必要性 三.如何在Bean创建之前提前创建代 ...

随机推荐

  1. redis-cli启动问题

    首先需要找到redis的所在目录,然后将redis.conf复制到/etc/redis.conf 另外需要将redis.conf文件中的 daemonize no 设置为 daemonize yes. ...

  2. Ad Infinitum 8 - Math Programming Contest

    比赛链接 A题 如果当前数是1,那么后面无论是几都会加1或者当后面数是1的时候加2,所以记录一下后面的数中1的个数(加2)即可. 如果当前数是2,那么只有当后面的数为1或者为2时才可以加1,所以再记录 ...

  3. XSS“从1到0”

    时隔半年终于也应该更新了,之前说的每周更新也因为懒散这个借口变得遥遥无期.之所以叫这个标题,是在Freebuf上看到一篇文章,开头作者问到:“网上大多的文章标题都是XXX从0开始,可我们到底什么时候能 ...

  4. 关于apache kylin 安装32位linux办法

    最近公司在使用apache kylin做实时开发访问,但是自己集群是linux32 只能安装JDK32位的受限只能3G多内存,而apachekylin 默认是4G内存,需要JDK64位支持. 解决办法 ...

  5. spring整合http

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接出处:https://blog.csdn.net/qq_3076499,否则保留追究法律责任的权利. 如果 ...

  6. mac 安装svn

    别人说用Xcode装,我也不知道我这个是不是用Xcode装的 在命令行界面输入 sudo bash svn --version 会出现一大段介绍,关于xcode的,我也不懂,一只敲空格键到最后,然后输 ...

  7. nginx的四个基本功能

    Nginx能做什么 1.反向代理2.负载均衡3.HTTP服务器(包含动静分离)4.正向代理 以上就是做网站小编了解到的Nginx在不依赖第三方模块能处理的事情,下面详细说明每种功能怎么做 1.反向代理 ...

  8. 读书笔记--Struts 2 in Action 目录

    1.Struts 2:现代Web框架 1.1 web应用程序:快速学习 21.1.1 构建web应用程序 21.1.2 基础技术简介 31.1.3 深入研究 61.2 web应用程序框架 71.2.1 ...

  9. 配置 CentOS 7 的网络,及重命名网卡名

    Centos 安装时应配置网络,如果当时没配置好,则装完系统后, 也可通过修改配置文件并重启网络服务进行配置. 说明:CentOS 7.0默认安装好之后是没有自动开启网络连接的! cd /etc/sy ...

  10. python禁止函数修改列表的实现方法

    python禁止函数修改列表的实现方法 有时候,需要禁止函数修改列表.例如要对裂变进行修改操作,也要保留原来的未打印的设计列表,以供备案.为解决这个问题,可向函数传递列表的副本而不是原件:这样函数所做 ...