Spring Mvc那点事---(30)Spring Mvc传统AOP自动代理实现

来源:互联网 发布:王兀 知乎 编辑:程序博客网 时间:2024/05/16 07:39
        Spring 传统AOP可以实现自动代理,不需要专门指定代理,可以在类生成的时候自动代理,有两种方式实现自动代理,基于Bean名称的自动代理
BeanNameAutoProxyCreator和基于切面信息的自动代理DefaultAdvisorAutoProxyCreator

1.BeanNameAutoProxyCreator

 

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


beanaop.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">        <bean id="goodsService" class="com.springdemo.aopdeom.GoodsService"></bean>    <bean id="beforeAdvice" class="com.springdemo.aopdeom.BeforeAdvice"/>        <!-- 基于Bean名称的自动代理 -->    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    <!-- 拦截的业务bean -->        <property name="beanNames" value="*goodsService"/>        <!-- 拦截的通知 -->        <property name="interceptorNames" value="beforeAdvice"/>    </bean></beans>


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/beanaop.xml"); GoodsService goodsService=(GoodsService)context.getBean("goodsService"); goodsService.Add(); goodsService.Update();        }}


2.DefaultAdvisorAutoProxyCreator


defaultaop.xml

<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="goodsService" class="com.springdemo.aopdeom.GoodsService"></bean>    <bean id="beforeAdvice" class="com.springdemo.aopdeom.BeforeAdvice"/>              <!-- 配置切面信息 -->    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">        <property name="advice" ref="beforeAdvice"/>           <property name="pattern" value=".*Add.*"/> <!-- 匹配添加方法 -->    </bean>        <!-- 配置基于切面信息自动代理 -->    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/></beans>

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/defaultaop.xml");    GoodsService goodsService=(GoodsService)context.getBean("goodsService");    goodsService.Add();       goodsService.Update();          }}


0 0
原创粉丝点击