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. 小米手机root

    目录 概念 解锁流程 root流程 如何Root? 关于supersu 关于twrp 关于Magisk Manager ref: 申请开发板流程 线刷教程 小米手机root 概念 解锁: 使手机可以刷 ...

  2. 请问在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句?

    using dotNET.Core; using Microsoft.Extensions.Logging; using System; using System.Collections.Generi ...

  3. vue---将json导出Excel的方法

    在做数据表格渲染的时候,经常遇到的需求的就是将导出excel,下面是使用vue进行项目数据进行导出的方法. 一.安装依赖 npm i -S file-saver npm i -S xlsx 二.在sr ...

  4. openvswitch2.11.0修改源码后重新编译

    一:推文 https://www.jianshu.com/p/923f49c290f5(可以删除运行当中的DataPath内核) https://github.com/ebiken/doc-netwo ...

  5. PageRank算法原理与Python实现

    一.什么是pagerank PageRank的Page可是认为是网页,表示网页排名,也可以认为是Larry Page(google 产品经理),因为他是这个算法的发明者之一,还是google CEO( ...

  6. springboot 读取配置文件

    读取配置文件 在以前的项目中我们主要在 XML 文件中进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息. 在 Spring Boot 中我们不再需要使用这种方式 ...

  7. 解决 Yii2 assets 不自动更新问题

    问题描述:core 里的 Asset (AssetBundle)更新 js 或 css 时,更新内容没有直接同步到其他模块 -- 如果想节约时间,直接拖到文章底部看结果就好~ 一.项目目录结构(大概介 ...

  8. RabbitMQ中文文档PHP版本(三)--工作队列

    2019年12月10日09:57:52 原文:https://www.rabbitmq.com/tutorials/tutorial-two-php.html

  9. laravel操作mongo详细说明

    原文地址:http://returnc.com/detail/3728   一个Eloquent模型和Query构建器,支持MongoDB,使用原始的Laravel API.该库扩展了原始的Larav ...

  10. Redis Sentinel 高可用部署实践集群

    一.Redis Sentinel 介绍    1.Sentinel     数据库环境搭建,从单机版到主备.再到多数据库集群,我们需要一个高可用的监控:比如Mysql中,我们可能会采用MHA来搭建我们 ...