Spring AOP-05-introduction enhanced IntroductionInterceptor

Posted by gabaod on Wed, 29 Jan 2020 17:41:29 +0100

The goal of reference enhancement is to add some new methods and properties to the target class. Take the Waiter class as an example. Now you want to add a manage() method in the Management interface to it without modifying the code of the Waiter class.

1. Classes to enhance

package com.test.springadvicetype;

import org.springframework.stereotype.Component;

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

/**
 * Attendants
 */
@Component
public class Waiter {
    /**
     * service
     * @param name
     */
    public String serve(String name) {
        System.out.println(name + ",Hello, glad to serve you.");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return name + ",Hello, it's Beijing time" + format.format(new Date());
    }

    /**
     * Drive a car
     * @param name
     */
    public void driving(String name) {
        throw new RuntimeException(name + ",Hello, no drunk driving!");
    }
}

2. Enhance new class Management for Waiter

package com.test.springadvicetype.introductioninterceptor;

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

3. The implementation class of Management, DelegatingIntroductionInterceptor, has implemented the introduction enhanced interface IntroductionInterceptor, which is directly inherited.

package com.test.springadvicetype.introductioninterceptor;

import org.springframework.aop.support.DelegatingIntroductionInterceptor;

/**
 * Managers
 */
public class Manager extends DelegatingIntroductionInterceptor implements Management {

    @Override
    public void manage(String name) {
        System.out.println(name + ",Hello, I'm the manager. I'm in charge of the waiter");
    }
}

4. XML configuration, 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">
    <!-- Enable annotation scanning -->
    <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. Test code

package com.test.springadvicetype.introductioninterceptor;

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

/**
 * Spring Introduction enhancement test
 */
public class SpringIntroductionInterceptorDemo {

    public static void main(String[] args) {
        System.out.println("Spring Introduction enhancement test");
        System.out.println("==========I'm the divider==========");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springintroductioninterceptor.xml");
        Waiter waiterProxy = (Waiter) context.getBean("waiterProxy");
        waiterProxy.serve("Michael");
        System.out.println("==========I'm the divider==========");
        Management manager = (Management)waiterProxy;
        manager.manage("Michael");
    }
}

6. Operation results

Spring surround enhancement test
==========I'm the divider==========
Pre enhancement performed
Hi Michael, I'm glad to serve you.
Post enhancement performed
==========I'm the divider==========
Pre enhancement performed
Hello, Tommy. I'm glad to serve you.
Post enhancement performed

Topics: Programming Spring xml Java encoding