1. 程式人生 > >Spring -- org.springframework.aop.framework.ProxyFactoryBean

Spring -- org.springframework.aop.framework.ProxyFactoryBean

配置

<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
>
<bean id="testAdvisor" class="com.redhorse.service.TestAdvisor"></bean> <bean id="testAOP" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.redhorse.test.AbcInterface</value> </property
>
<property name="target"> <bean class="com.redhorse.test.AbcInterfaceImpl"></bean> </property> <property name="interceptorNames"> <list> <value>testAdvisor</value> </list>
</property> </bean> </beans>

程式碼

業務類

/**     
 * @Title:  AbcInterface.java   
 * @Package com.redhorse.test   
 * @Description:    TODO(用一句話描述該檔案做什麼)   
 * @author: Gao Peng     
 * @date:   2016年6月12日 上午11:00:22       
 */ 
package com.redhorse.test;

/**   
 * @ClassName:  AbcInterface   
 * @Description:TODO(這裡用一句話描述這個類的作用)   
 * @author: Gao Peng  
 * @date:   2016年6月12日 上午11:00:22   
 *      
 */
public interface AbcInterface {

    public void test();
}
/**     
 * @Title:  AbcInterfaceImpl.java   
 * @Package com.redhorse.test   
 * @Description:    TODO(用一句話描述該檔案做什麼)   
 * @author: Gao Peng     
 * @date:   2016年6月12日 上午11:12:21       
 */
package com.redhorse.test;

/**
 * @ClassName: AbcInterfaceImpl
 * @Description:TODO(這裡用一句話描述這個類的作用)
 * @author: Gao Peng
 * @date: 2016年6月12日 上午11:12:21
 * 
 */
public class AbcInterfaceImpl implements AbcInterface {

    /*
     * (non-Javadoc)
     * 
     * @see com.redhorse.test.AbcInterface#test()
     */
    @Override
    public void test() {
        System.out.println("abcInterface handle...");
    }

}
/**     
 * @Title:  TestAdvisor.java   
 * @Package com.redhorse.service   
 * @Description:    TODO(用一句話描述該檔案做什麼)   
 * @author: Gao Peng     
 * @date:   2016年6月12日 上午11:10:38       
 */
package com.redhorse.service;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Service;

/**
 * @ClassName: TestAdvisor
 * @Description:TODO(這裡用一句話描述這個類的作用)
 * @author: Gao Peng
 * @date: 2016年6月12日 上午11:10:38
 * 
 */
@Service
public class TestAdvisor implements AfterReturningAdvice{

    /* (non-Javadoc)
     * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
     */
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("begin afterReturning ...");
    }

}