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
原创粉丝点击