SpringAOP05引介增强IntroductionInterceptor

编程

1、需要增强的类

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 + ",您好,禁止酒后驾车!");

}

}

2、为Waiter增强新的类 Management

package com.test.springadvicetype.introductioninterceptor;

public interface Management {

/**

* 管理

* @param name

*/

void manage(String name);

}

3、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 + ",您好,我是经理,负责管理服务员");

}

}

4、xml配置,spring-chapter3-springintroductioninterceptor.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"

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>

5、测试代码

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

}

}

6、运行结果

Spring环绕增强测试

==========我是分割线==========

前置增强执行了

Michael,您好,很高兴为您服务。

后置增强执行了

==========我是分割线==========

前置增强执行了

Tommy,您好,很高兴为您服务。

后置增强执行了

 

以上是 SpringAOP05引介增强IntroductionInterceptor 的全部内容, 来源链接: utcz.com/z/513041.html

回到顶部