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(); } }

最新文章

  1. jQuery验证插件 Validate详解
  2. 【BZOJ-3238】差异 后缀数组 + 单调栈
  3. iOS多线程GCD(转)
  4. lvchange的available參数
  5. Qt中QWidget加入到父类QWidget不能显示的问题
  6. Android提高第二篇之SurfaceView的基本使用
  7. 关于centos连接mssql的问题
  8. linux基础 作业篇
  9. 空间金字塔池化(Spatial Pyramid Pooling, SPP)原理和代码实现(Pytorch)
  10. 开发你的第一个BLE应用程序—Blinky
  11. nginx代理tcp协议连接mysql
  12. Linux下设置svn开机自启动
  13. Python switch(多分支选择)的实现
  14. [CB]2018全球半导体营收4700亿美元 三星继续碾压英特尔
  15. 探秘Tomcat(一)
  16. lua的文件管理
  17. Linux Mint KDE上安装fcitx+sougou输入法
  18. Discoverer Table
  19. @RequestParam和@RequestBody的区别-------springMVC
  20. Arduino I2C + 温湿度传感器Si7021

热门文章

  1. 【arc072f】AtCoder Regular Contest 072 F - Dam
  2. Vue.之.回到顶部
  3. HTML5:使用Canvas和Input range控件放大缩小图片,剪裁,并上传图片
  4. webservice作用(优,缺点;作用)
  5. jmeter断言步骤
  6. 对比Model前后数据保存不同值
  7. ACMer之歌
  8. Ubuntu 16.04 配置 L2tp 客户端
  9. Javascript实现信息滚动效果的方法
  10. Codeforces 356A