1. 程式人生 > >Spring Mvc那點事---(30)Spring Mvc傳統AOP自動代理實現

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

    }
}