Spring AOP-05-引介加强 IntroductionInterceptor

引介加强的目标是在目标类中添加一些新的方法和属性。以Waiter类为例,如今想给其添加一个Management接口中的manage()方法而不修改Waiter类的代码。java

一、须要加强的类spring

package com.test.springadvicetype;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 服务员类
 */
@Component
public class Waiter {
    /**
     * 服务
     * @param name
     */
    public String serve(String name) {
        System.out.println(name + ",您好,很高兴为您服务。");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return name + ",您好,如今是北京时间" + format.format(new Date());
    }

    /**
     * 开车
     * @param name
     */
    public void driving(String name) {
        throw new RuntimeException(name + ",您好,禁止酒后驾车!");
    }
}

二、为Waiter加强新的类 Managementide

package com.test.springadvicetype.introductioninterceptor;

public interface Management {
    /**
     * 管理
     * @param name
     */
    void manage(String name);
}

三、Management 的实现类,DelegatingIntroductionInterceptor已经实现了引介加强接口IntroductionInterceptor,直接继承。测试

package com.test.springadvicetype.introductioninterceptor;

import org.springframework.aop.support.DelegatingIntroductionInterceptor;

/**
 * 经理类
 */
public class Manager extends DelegatingIntroductionInterceptor implements Management {

    @Override
    public void manage(String name) {
        System.out.println(name + ",您好,我是经理,负责管理服务员");
    }
}

四、xml配置,spring-chapter3-springintroductioninterceptor.xmlspa

<?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"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.test.springadvicetype"/>

    <bean id="waiter" class="com.test.springadvicetype.Waiter"/>

    <bean id="manager2" class="com.test.springadvicetype.introductioninterceptor.Manager"/>

    <bean id="waiterProxy" class="org.springframework.aop.framework.ProxyFactoryBean"
          p:interfaces="com.test.springadvicetype.introductioninterceptor.Management"
          p:interceptorNames="manager2"
          p:target-ref = "waiter"
          p:proxyTargetClass="true"/>
</beans>

五、测试代码code

package com.test.springadvicetype.introductioninterceptor;

import com.test.springadvicetype.Waiter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring引介加强测试
 */
public class SpringIntroductionInterceptorDemo {

    public static void main(String[] args) {
        System.out.println("Spring引介加强测试");
        System.out.println("==========我是分割线==========");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springintroductioninterceptor.xml");
        Waiter waiterProxy = (Waiter) context.getBean("waiterProxy");
        waiterProxy.serve("Michael");
        System.out.println("==========我是分割线==========");
        Management manager = (Management)waiterProxy;
        manager.manage("Michael");
    }
}

六、运行结果component

Spring环绕加强测试
==========我是分割线==========
前置加强执行了
Michael,您好,很高兴为您服务。
后置加强执行了
==========我是分割线==========
前置加强执行了
Tommy,您好,很高兴为您服务。
后置加强执行了
相关文章
相关标签/搜索