Spring AOP-01 pre enhanced MethodBeforeAdvice

Keywords: Programming Spring xml Java encoding

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 before method calling to implement the MethodBeforeAdvice interface

package com.test.springadvicetype.afterretuningadvice;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Pre enhanced implementation class
 */
@Component
public class SpringBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        String methodName = method.getName();
        System.out.printf("MethodBeforeAdvice The way to enhance it%s%n", methodName);
        System.out.printf("MethodBeforeAdvice The parameters of the enhanced method are%s%n", args[0]);
        System.out.printf("MethodBeforeAdvice The enhanced objects are%s%n", target);
    }
}

3. XML configuration, spring-chapter3-springaoptype.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"
       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"/>
</beans>

4. Run program

package com.test.springadvicetype.beforeadvice;

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

/**
 * Spring Pre enhancement test
 */
public class SpringBeforeAdviceDemo {

    public static void main(String[] args) {
        System.out.println("Spring Pre enhancement test");
        System.out.println("==========I'm the divider==========");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springaoptype.xml");
        Waiter waiter = (Waiter) context.getBean("waiter");
        SpringBeforeAdvice advice = (SpringBeforeAdvice) context.getBean("springBeforeAdvice");
        //Proxy factory provided by Spring
        ProxyFactory pf = new ProxyFactory();
        //Set proxy target
        pf.setTarget(waiter);
        pf.addAdvice(advice);
        //Generate agent instance
        Waiter proxy = (Waiter)pf.getProxy();
        proxy.serve("Michael");
        System.out.println("==========I'm the divider==========");
        proxy.serve("Tommy");
    }
}

5. Operation results

Spring pre enhancement test
==========I'm the divider==========
MethodBeforeAdvice the enhanced method is serve
MethodBeforeAdvice the parameter of the enhanced method is Michael
MethodBeforeAdvice is enhanced to com.test.springadvicetype.Waiter@5276e6b0
Hi Michael, I'm glad to serve you.
==========I'm the divider==========
MethodBeforeAdvice the enhanced method is serve
The parameter of MethodBeforeAdvice enhanced method is Tommy
MethodBeforeAdvice is enhanced to com.test.springadvicetype.Waiter@5276e6b0
Hello, Tommy. I'm glad to serve you.

Posted by Emperio on Tue, 28 Jan 2020 07:50:07 -0800