Spring Mvc那点事---(29)Spring Mvc基于ProxyFactoryBean的传统AOP使用正则拦截
来源:互联网 发布:淘宝无线端链接地址 编辑:程序博客网 时间:2024/04/29 13:28
上一节中,我们介绍ProxyFactoryBean拦截的时候,切面需要继承NameMatchMethodPointcut接口,而且业务对象也要使用接口。这节我们看看怎么使用正则表达式来实现,并且业务对象不需要继承接口,默认可以使用cglib代理来实现。
jar包
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.5.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.5.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version></dependency>
创建业务对象
package com.springdemo.aopdeom;public class GoodsService { public void Add(){System.out.println("商品添加");}public void Update(){System.out.println("商品修改");}}
创建前置通知,和上一节的一样
package com.springdemo.aopdeom;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;//前置通知public class BeforeAdvice implements MethodBeforeAdvice {//参数1是目标方法//参数2是方法参数//参数3是目标对象public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {// TODO Auto-generated method stub System.out.println("执行前:目标对象"+arg2.getClass().getName()+",方法"+arg0.getName());}}
配置文件,使用RegexMethodPointCutAdvisor进行拦截,可以设置正则表达式来匹配指定的对象,可以设置通知
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <bean id="goodsService" class="com.springdemo.aopdeom.GoodsService" /> <bean id="beforeAdvice" class="com.springdemo.aopdeom.BeforeAdvice" /> <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!-- <property name="pattern" value=".*"/> .代表任意数字,*代表任意次数--> <property name="pattern" value=".*Add.*"/> <!-- 匹配添加方法 --> <property name="advice" ref="beforeAdvice"/> </bean> <bean id="goodServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="goodsService"/> <property name="proxyTargetClass" value="true"/> <property name="interceptorNames" value="advisor"/> </bean></beans><property name="proxyTargetClass" value="true"/>表示强制使用cglib代理。
package com.springdemo.aopdeom;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/springdemo/aopdeom/PattenAop.xml"); GoodsService goodsService=(GoodsService)context.getBean("goodServiceProxy"); goodsService.Add(); goodsService.Update(); }
0 0
- Spring Mvc那点事---(29)Spring Mvc基于ProxyFactoryBean的传统AOP使用正则拦截
- Spring Mvc那点事---(28)Spring Mvc基于ProxyFactoryBean的传统AOP
- Spring Mvc那点事---(26)Spring Mvc基于Schema的AOP实现
- Spring Mvc那点事---(27)Spring Mvc基于aspect的AOP实现
- Spring Mvc那点事---(30)Spring Mvc传统AOP自动代理实现
- Spring Mvc那点事---(21)Spring Mvc拦截器
- Spring Mvc那点事---(33)Spring事务基于AOP实现
- Spring Mvc那点事---(22)Spring Mvc拦截器之WebRequestInterceptor
- Spring Mvc那点事---(40)SSM服务框架使用aop进行数据验证
- Spring AOP之基于ProxyFactoryBean的代理
- Spring MVC的中如何使用AOP拦截到@控制器?
- Spring Mvc那点事---(3)Spring Mvc项目创建
- Spring Mvc那点事---(4)Spring Mvc控制器Controller
- Spring Mvc那点事---(7)Spring Mvc @Autowired注解
- Spring Mvc那点事---(8)Spring Mvc @Resource注解
- Spring Mvc那点事---(9)Spring Mvc @Component注解
- Spring Mvc那点事---(18)Spring Mvc过滤器Filter
- Spring Mvc那点事---(23)Spring Mvc监听器
- SXY逛公园
- Android将String类型字符串转换成utf-8,gbk格式
- 训练第四天之贪心算法
- RT-Thread互斥锁
- js信息滚动marquee标签
- Spring Mvc那点事---(29)Spring Mvc基于ProxyFactoryBean的传统AOP使用正则拦截
- Java 入门 多线程 之 有返回值的线程(一)
- Python_PIL 打开转存图片
- [leetcode]maximum-depth-of-binary-tree C++
- 赌场概率论
- TCP/IP协议栈的网络层的主要功能是通过( )来完成的。
- 数据结构——链表表的实现
- Docker中使用Python Notebook
- 绘制渐变图形