AOP实现方式1——经典的基于代理的AOP实现

原文:http://blog.csdn.net/csdn_kenneth/article/details/69390064

1.定义接口Perform

package com.show;

/**
 * Created by kenneth on 2017/4/6.
 */
public interface Perform {
    void sing();
}
2.定义接口Perform的实现类Boy

package com.show;

/**
 * Created by kenneth on 2017/4/6.
 */
public class Boy implements Perform {
    @Override
    public void sing() {
        System.out.println("男孩在唱歌");
    }
}

3.定义通知PerformHelper

package com.show;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by kenneth on 2017/4/6.
 */
public class PerformHelper implements MethodBeforeAdvice, AfterReturningAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("表演之前要整理衣服");
    }

    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("表演之后要行礼");
    }
}

4.配置Spring配置文件

<?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/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <!-- 定义被代理对象 -->
    <bean id="boy" class="com.show.Boy"></bean>

    <!-- 定义通知内容,也就是切入点执行前后需要做的事情 -->
    <bean id="performHelper" class="com.show.PerformHelper"></bean>

    <!--定义切入点位置-->
    <bean id="performPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*sing"/>
    </bean>

    <!--使切入点和通知相关联,完成切面配置-->
    <bean id="performHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="performHelper"/>
        <property name="pointcut" ref="performPointCut"/>
    </bean>

    <!--设置代理-->
    <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--代理的对象-->
        <property name="target" ref="boy"/>
        <!--使用切面-->
        <property name="interceptorNames" value="performHelperAdvisor"/>
        <!--代理接口-->
        <property name="proxyInterfaces" value="com.show.Perform"/>
    </bean>
</beans>

5.测试类

package com.show;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by kenneth on 2017/4/6.
 */
public class TestPerform {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Perform performer = (Perform) context.getBean("proxy");
        performer.sing();
    }
}
6.测试结果:

四月 06, 2017 3:25:44 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@533ddba: startup date [Thu Apr 06 15:25:44 CST 2017]; root of context hierarchy
四月 06, 2017 3:25:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicationContext.xml]
表演之前要整理衣服
男孩在唱歌
表演之后要行礼

发布了14 篇原创文章 · 获赞 8 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/langwuzhe/article/details/78808746