day39-Spring 05-Spring的AOP:不带有切点的切面
Spring底层的代理的实现:







不带切点的切面是对类里面的所有的方法都进行拦截.
做Spring AOP的开发需要两个包:一个是AOP的包,一个是AOP联盟的包(因为规范是由AOP联盟提出来的).



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





ProxyFactoryBean会帮你自动生成代理.





通过配置就可以让Spring自动生成代理对象了.这就是不带切点的切面.但是这种方式以后几乎不会用,因为你有一个类就得配置一段,你要是有一百个类就得配置一百次下面这段代码
<!-- 手动生成代理太麻烦了.Spring支持配置生成代理: -->
<!-- 代理对象 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象 -->
<property name="target" ref="customerDao"></property>
<!-- 设置实现的接口,value中写接口的全路径 -->
<!-- 类实现的接口的全路径 -->
<property name="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"></property>
<!-- 需要使用value:要的名称 使用beforeAdvice对它进行增强 -->
<property name="interceptorNames" value="beforeAdvice"></property><!--interceptorNames要拦截的名称 -->
<!-- 这个是针对类的所有方法进行拦截的,你还没到配置方法的那一步呢 -->
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入beans的头 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义目标对象 -->
<bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl"> </bean>
<!-- 定义增强 增强对象-->
<bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice">
</bean> <!-- 手动生成代理太麻烦了.Spring支持配置生成代理: -->
<!-- 代理对象 -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象 -->
<property name="target" ref="customerDao"></property>
<!-- 设置实现的接口,value中写接口的全路径 -->
<!-- 类实现的接口的全路径 -->
<property name="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"></property>
<!-- 需要使用value:要的名称 使用beforeAdvice对它进行增强 -->
<property name="interceptorNames" value="beforeAdvice"></property><!--interceptorNames要拦截的名称 -->
<!-- 这个是针对类的所有方法进行拦截的,你还没到配置方法的那一步呢 -->
</bean>
</beans>
package cn.itcast.spring3.demo3;
public interface CustomerDao {
public void add();
public void delete();
public void update();
public void find();
}
package cn.itcast.spring3.demo3;
public class CustomerDaoImpl implements CustomerDao {
public void add() {
// TODO Auto-generated method stub
System.out.println("添加客户");
}
public void delete() {
// TODO Auto-generated method stub
System.out.println("删除客户");
}
public void update() {
// TODO Auto-generated method stub
System.out.println("修改客户");
}
public void find() {
// TODO Auto-generated method stub
System.out.println("查询客户");
}
}
package cn.itcast.spring3.demo3; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /**
* 前置增强
* @author zhongzh
*
*/
public class MyBeforeAdvice implements MethodBeforeAdvice{
/*
* method:执行的方法
* args:参数
* target:目标对象
* 要对目标对象的某一方法增强
* (non-Javadoc)
* @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
*/
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("前置增强.....");
} }
package cn.itcast.spring3.demo3; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTest3 {
@Autowired
//@Qualifier("customerDao")//和applicationContext.xml中配置的id=customerDao对应 注入的是真实的对象,必须注入代理对象.
@Qualifier("customerDaoProxy")
private CustomerDao customerDao; @Test
public void demo1(){
customerDao.add();
customerDao.delete();
customerDao.update();
customerDao.find(); } }
day39-Spring 05-Spring的AOP:不带有切点的切面的更多相关文章
- day39-Spring 06-Spring的AOP:带有切点的切面
环绕增强功能是最强的,它相当于前置增强和后置增强. 这就是带有切点的切面 package cn.itcast.spring3.demo4; import org.aopalliance.interce ...
- Spring的IOC和AOP之深剖
今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- Spring基础学习(四)—AOP
一.AOP基础 1.基本需求 需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...
- Spring总结六:AOP(面向切面编程)
概述: AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是一种新的 ...
- Spring AOP(通知、连接点、切点、切面)
一.AOP术语 通知(Advice) 切面的工作被称为通知.通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题.5种通知类型: 前置通知(Before): ...
- Spring MVC 中使用AOP 进行统一日志管理--注解实现
1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...
- 【Spring】每个程序员都使用Spring(四)——Aop+自定义注解做日志拦截
一.前言 上一篇博客向大家介绍了Aop的概念,对切面=切点+通知 .连接点.织入.目标对象.代理(jdk动态代理和CGLIB代理)有所了解了.理论很强,实用就在这篇博客介绍. 这篇博客中,小编向大家介 ...
- 这一次搞懂Spring代理创建及AOP链式调用过程
文章目录 前言 正文 基本概念 代理对象的创建 小结 AOP链式调用 AOP扩展知识 一.自定义全局拦截器Interceptor 二.循环依赖三级缓存存在的必要性 三.如何在Bean创建之前提前创建代 ...
随机推荐
- ArccGIS 10发布WFS服务并加载到Skyline中
下面用ArcGIS Server 10.0将建筑物图层发布为WFS服务. (1)创建mxd文件.ArcMap打开建筑物图层,存为Buildings.mxd文件.注意:必须统一空间参考系,且要与图层的坐 ...
- crontab定时任务语法及应用
https://mp.weixin.qq.com/s/Oi9hppNQMeFiQo9s-ge79A crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows ...
- 加载框(loading)
一般在用户提交数据或者新加载页面时,请求服务器的过程,页面没有响应,但是用户并不知道,此时在发生什么.这时,就需要loading框给用户提示,增加用户体验. 1.引入loading.css. html ...
- PAT甲级——A1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
- bootstrap-table的简要使用
bttable功能强大!同时支持申明方式和编程方式来使用!是wenzhixin主导开发一款开源的表格控件! 文档比较详尽,但要求初学者已经比较熟悉js,jquey等基本内容,否则可能许多代码无法阅读! ...
- CentOS 6.8 Linux系统U盘制作启动项
1.下载CentOS 6.8镜像文件: 2.下载地址:http://man.linuxde.net/download/CentOS_6_8 3.准备一个U盘,最好8G的: 4.下载UltraISO盘制 ...
- Linux中如何安装mysql数据库
安装mysql 1.解压源码压缩包 如果服务器可以上网也可以采用在线安装方式,在线安装操作简单具体见下面在线安装步骤 进入源码压缩包所在目录输入#tar -zxvf mysql-5.6.17-linu ...
- JasperReport报表设计4
在JRXML模板(或JRXML文件)中的JasperReport 都是标准的 XML文件,以.JRXML扩展.所有JRXML文件包含标签<jasperReport>,作为根元素.这反过来又 ...
- 2019-4-17-从-dotnet-core-3.0-的特性让-WPF-布局失效讨论-API-兼容
title author date CreateTime categories 从 dotnet core 3.0 的特性让 WPF 布局失效讨论 API 兼容 lindexi 2019-4-17 1 ...
- LINUX设置SUID,SGID,Stick bit
前面介绍过SUID与SGID的功能,那么,如何打开文件使其成为具有SUID与SGID的权限呢?这就需要使用数字更改权限了.现在应该知道,使用数字 更改权限的方式为“3个数字”的组合,那么,如果在这3个 ...