Spring AOP-01-前置增强 MethodBeforeAdvice

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、方法调用前增强,实现 MethodBeforeAdvice 接口

package com.test.springadvicetype.afterretuningadvice;

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

import java.lang.reflect.Method;

/**
 * 前置增强实现类
 */
@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增强的方法是%s%n", methodName);
        System.out.printf("MethodBeforeAdvice增强的方法的参数是%s%n", args[0]);
        System.out.printf("MethodBeforeAdvice增强的对象是%s%n", target);
    }
}

3、xml配置,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">
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.test.springadvicetype"/>
</beans>

4、运行程序

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前置增强测试
 */
public class SpringBeforeAdviceDemo {

    public static void main(String[] args) {
        System.out.println("Spring前置增强测试");
        System.out.println("==========我是分割线==========");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springaoptype.xml");
        Waiter waiter = (Waiter) context.getBean("waiter");
        SpringBeforeAdvice advice = (SpringBeforeAdvice) context.getBean("springBeforeAdvice");
        //Spring提供的代理工厂
        ProxyFactory pf = new ProxyFactory();
        //设置代理目标
        pf.setTarget(waiter);
        pf.addAdvice(advice);
        //生成代理实例
        Waiter proxy = (Waiter)pf.getProxy();
        proxy.serve("Michael");
        System.out.println("==========我是分割线==========");
        proxy.serve("Tommy");
    }
}

5、运行结果

Spring前置增强测试
==========我是分割线==========
MethodBeforeAdvice增强的方法是serve
MethodBeforeAdvice增强的方法的参数是Michael
MethodBeforeAdvice增强的对象是com.test.springadvicetype.Waiter@5276e6b0
Michael,您好,很高兴为您服务。
==========我是分割线==========
MethodBeforeAdvice增强的方法是serve
MethodBeforeAdvice增强的方法的参数是Tommy
MethodBeforeAdvice增强的对象是com.test.springadvicetype.Waiter@5276e6b0
Tommy,您好,很高兴为您服务。