AOP之ProxyFactoryBean

Person.java

/**
 * Created by wangrui04 on 2016/3/31.
 */
public interface Person {
    void say();
    void run();
}


Student.java

/**
 * Created by wangrui04 on 2016/3/31.
 */
public class Student implements Person {

    public void say() {
        System.out.println("hello world!");
    }

    public void run(){
        System.out.println("run fast!");
    }
}

ProxyStudent.java

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by wangrui04 on 2016/3/31.
 */
public class ProxyStudent implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("before");
        Object result = invocation.proceed();
        System.out.println("after");
        return result;
    }
}

Spring-test.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="student" class="Student"/>
    <bean id="interceptor" class="ProxyStudent"/>

    <bean id="myPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
        <!-- 只对say方法拦截 -->
        <property name="mappedName" value="say"/>
    </bean>

    <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="interceptor"/>
        <property name="pointcut" ref="myPointcut"/>
    </bean>

    <bean id="proxyStudent" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="interfaces">
            <list>
                <value>Person</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <!--<value>interceptor</value>-->
                <value>myAdvisor</value>
            </list>
        </property>
        <property name="target" ref="student"/>
    </bean>
</beans>

BootMain.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

/**
 * Created by wangrui04 on 2016/3/31.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-test.xml"})
public class BootMain {

    @Resource(name = "proxyStudent")
    private Person student;

    @Test
    public void testMain(){
        student.say();
        student.run();
    }
}

output

before
hello world!
after
run fast!
  • Advice –Indicate the action to take either before or after the method execution.

  • Pointcut – Indicate which method should be intercept, by method name or regular expression pattern.

  • Advisor – Group ‘Advice’ and ‘Pointcut’ into a single unit, and pass it to a proxy factory object.

  • 可以通过自定义advisor,指定pointcut中的mapperName来实现只有指定的方法才会被拦截.

  • 对每一个需要代理的bean都需要在xml中配置,太繁琐,应该使用AspectJ的方式.

你可能感兴趣的:(AOP之ProxyFactoryBean)