SpringAOP02后置增强AfterReturningAdvice

编程

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、后置增强,实现 AfterReturningAdvice 接口

package com.test.springadvicetype.afterretuningadvice;

import org.springframework.aop.AfterReturningAdvice;

import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**

* 后置增强

*/

@Component

public class SpringAfterReturningAdvice implements AfterReturningAdvice {

@Override

public void afterReturning(Object returnValue, Method method, Object[] args, Object target)

throws Throwable {

String methodName = method.getName();

System.out.printf("AfterReturningAdvice增强的方法返回值是:%s%n", returnValue);

System.out.printf("AfterReturningAdvice增强的方法是:%s%n", methodName);

System.out.printf("AfterReturningAdvice增强的方法的参数是:%s%n", args[0]);

System.out.printf("AfterReturningAdvice增强的对象是:%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.afterretuningadvice;

import org.springframework.aop.AfterReturningAdvice;

import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**

* 后置增强

*/

@Component

public class SpringAfterReturningAdvice implements AfterReturningAdvice {

@Override

public void afterReturning(Object returnValue, Method method, Object[] args, Object target)

throws Throwable {

String methodName = method.getName();

System.out.printf("AfterReturningAdvice增强的方法返回值是:%s%n", returnValue);

System.out.printf("AfterReturningAdvice增强的方法是:%s%n", methodName);

System.out.printf("AfterReturningAdvice增强的方法的参数是:%s%n", args[0]);

System.out.printf("AfterReturningAdvice增强的对象是:%s%n", target);

}

}

5、运行结果

Spring后置增强测试

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

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

AfterReturningAdvice增强的方法返回值是:Michael,您好,现在是北京时间2020-01-28 10:27:56

AfterReturningAdvice增强的方法是:serve

AfterReturningAdvice增强的方法的参数是:Michael

AfterReturningAdvice增强的对象是:com.test.springadvicetype.Waiter@2e32ccc5

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

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

AfterReturningAdvice增强的方法返回值是:Tommy,您好,现在是北京时间2020-01-28 10:27:56

AfterReturningAdvice增强的方法是:serve

AfterReturningAdvice增强的方法的参数是:Tommy

AfterReturningAdvice增强的对象是:com.test.springadvicetype.Waiter@2e32ccc5

 

以上是 SpringAOP02后置增强AfterReturningAdvice 的全部内容, 来源链接: utcz.com/z/513044.html

回到顶部