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

        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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值