ProxyFactoryBean类

FactoryBean接口用于Bean的实例化,ProxyFactoryBean是它的一个实现类,用于实例化代理(Bean)。

spring  aop是用动态代理实现的,自己写jdk动态代理、cglib代理很麻烦,spring用ProxyFactoryBean封装了jdk动态代理、cglib动态代理,我们只需在xml中配置代理即可,不必手写动态代理。


Demo

(1)添加spring-aop.RELEASE.jar

Spring  AOP需要spring-aop.RELEASE.jar的支持。(待修改)

(2)目标接口、目标类

新建包com.chy.dao,包下新建接口UserDao、实现类UserDaoImpl:

public interface UserDao {
public void addUser();
public void deleteUser();
}
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("正在添加用户...");
} @Override
public void deleteUser() {
System.out.println("正在删除用户...");
}
}

(3)切面

新建包com.chy.aspect,包下新建类UserDaoAspect:

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class UserDaoAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
//前增强
System.out.println("正在执行前增强...");
//调用目标方法,返回值是Object类型
Object object=methodInvocation.proceed();
//后增强
System.out.println("正在执行后增强...");
//返回目标方法的返回值
return object;
}
}

注意实现的接口是spring-aop.RELEASE.jar中的aopalliance包下的接口:

import org.aopalliance.intercept.MethodInterceptor;

是spring aop封装好的接口,不必手写代理。

不是spring内嵌的cglib包下的接口:

import org.springframework.cglib.proxy.MethodInterceptor

spring内嵌了cglib需要的jar,这个MethodInterceptor是cglib的原生接口,需要手写动态代理。


实现相应的接口即可:

  通知类型
对应的接口
环绕通知     MethodInterceptor    
前置通知 MethodBeforeAdvice
后置通知

AfterAdvice(空接口)

异常通知 ThrowsAdvice(空接口)
返回通知 AfterReturningAdvice 

一般不使用空接口。

虽然AfterReturningAdvice是返回通知,但很多时候都可以作为后置通知使用。

示例     前增强

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method; public class UserDaoAspect implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前增强...");
}
}

(4)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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 目标类-->
<bean name="userDaoImpl" class="com.chy.dao.UserDaoImpl" /> <!-- 切面-->
<bean name="userDaoAspect" class="com.chy.aspect.UserDaoAspect" /> <!-- 配置ProxyFactoryBean类,用于生产代理对象-->
<bean name="userDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--指定要代理的接口,如果实现了多个接口,用子元素<list>来写-->
<property name="proxyInterfaces" value="com.chy.dao.UserDao" />
<!--指定目标对象-->
<property name="target" ref="userDaoImpl" />
<!--指定切面,只能用value,不能用ref-->
<property name="interceptorNames" value="userDaoAspect" />
<!-- 是否直接代理目标类
true:直接代理目标类,目标类不必实现接口,使用的是cglib动态代理
false:默认值,代理接口,目标类必须实现接口,使用的是jdk动态代理
-->
<property name="proxyTargetClass" value="false" />
<!--返回的代理对象是否使用单例,默认为true 单例-->
<property name="singleton" value="true" />
</bean>
</beans>

ProxyFactoryBean类封装好了创建代理的代码,我们只需使用<property>注入参数即可。

上面的配置代理的是目标接口,如果只代理目标类:

  • 不配置proxyInterfaces(不注入目标接口)
  • 将proxyTargetClass的值改为true(代理目标类)


(5)使用

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
UserDao user=applicationContext.getBean("userDaoProxy", UserDao.class);
user.addUser();

会自动增强方法。

基于代理类实现Spring AOP的更多相关文章

  1. Spring AOP(基于代理类的AOP实现)

    #基于代理类的AOP实现:step1: 1 package com.sjl.factorybean; /**切面类*/ import org.aopalliance.intercept.MethodI ...

  2. CgLib动态代理学习【Spring AOP基础之一】

    如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...

  3. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

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

    基于 Annotation 拦截的 Spring AOP 权限验证方法 转自:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilte ...

  5. Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式

    Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式 主题 概念 Hibernate 延迟加载的代理模式 Spring AOP的代理模式 区别和联系 静态代理和动态代理 概念 代 ...

  6. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  7. java代理课程测试 spring AOP代理简单测试

    jjava加强课程测试代码 反射. 代理 .泛型.beanUtils等 项目源码下载:http://download.csdn.net/detail/liangrui1988/6568169 热身运动 ...

  8. Java动态代理学习【Spring AOP基础之一】

    Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...

  9. 基于Aspectj 注解实现 spring AOP

    AOP 面向切面编程,是 OOP (面向对象编程)的补充 术语 横切关注点:方法中非主要业务逻辑部分 比如运算的模块:有验证参数.执行方法前的操作.执行方法.执行方法后的操作,验证参数.执行方法前后的 ...

随机推荐

  1. 微信小程序 获取地理位置信息

    app.json "permission":{ "scope.userLocation": { "desc": "你的位置信息将用 ...

  2. ES开启慢查询日志

    默认情况,慢日志是不开启的.要开启它,需要定义具体动作(query,fetch 还是 index),你期望的事件记录等级( WARN.INFO.DEBUG.TRACE 等),以及时间阈值. es有几种 ...

  3. ios兼容性收集整理

    1. ios系统兼input输入框光标问题 异常现象:苹果手机文本输入框样式异常——光标聚焦到文本框,光标高度充满文本框,输入内容,光标高度为文本框上边框到输入内容底部: 光标聚焦: 输入内容: 异常 ...

  4. 安装MySQL Server

    之前安装了MySQL Workbench 8.0 CE,现在来安装MySQL Server. 点击 add next next next     完成 MySQL安装包地址: 链接:https://p ...

  5. Python3基础 内置函数 eval str转为list tuple dict

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  6. 011-MySQL Query Cache 查询缓存设置操作

    一.概述 MySQL Query Cache 会缓存select 查询,安装时默认是开启的,但是如果对表进行INSERT, UPDATE, DELETE, TRUNCATE, ALTER TABLE, ...

  7. es查询和更新 语句示例

    文档目录: https://www.elastic.co/guide/index.html GET _search { "query": { "match_all&quo ...

  8. [UE4] Adding a custom shading model

    转自:https://blog.felixkate.net/2016/05/22/adding-a-custom-shading-model-1/ This was written in Februa ...

  9. prototype和__proto__的关系是什么?

    我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个对象,它的用途是包含可以由特定类型的所有实例共享的属性和方法.(这个对象下面有个属性,这个属性是另外一个对象的应用 ,这个属性就 ...

  10. ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)

    验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...