【Spring-AOP-学习笔记-5】@AfterReturning增强处理简单示例

项目结构


业务代码



@Component("hello")

public class HelloImpl implements Hello

{

// 定义一个简单方法,模拟应用中的业务逻辑方法

public void foo()

{

System.out.println("执行Hello组件的foo()方法");

}

// 定义一个addUser()方法,模拟应用中的添加用户的方法

public int addUser(String name , String pass)

{

System.out.println("执行Hello组件的addUser添加用户:" + name);

return 20;

}

}



@Component("world")

public class WorldImpl implements World

{

// 定义一个简单方法,模拟应用中的业务逻辑方法

public void bar()

{

System.out.println("执行World组件的bar()方法");

}

}


定义切面Bean



@Aspect

public class LogAspect

{

// 匹配org.crazyit.app.service.impl包下所有类的、

// 所有方法的执行作为切入点

@AfterReturning(returning="rvt"

, pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")

// 声明rvt时指定的类型会限制目标方法必须返回指定类型的值或没有返回值

// 此处将rvt的类型声明为Object,意味着对目标方法的返回值不加限制

public void log(Object rvt)

{

System.out.println("获取目标方法返回值:" + rvt);

System.out.println("模拟记录日志功能...");

}

}


说明:returing属性所指定的形参名必须对应增强处理中的一个形参名,当目标方法执行返回后,返回值作为相应的参数值传入增强处理方法中。
虽然AfterReturning增强处理可以访问到目标方法的返回值,但它不可以改变目标方法的返回值。

配置文件



<?xml version="1.0" encoding="GBK"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 指定自动搜索Bean组件、自动搜索切面类 -->

<context:component-scan base-package="org.crazyit.app.service

,org.crazyit.app.aspect">

<context:include-filter type="annotation"

expression="org.aspectj.lang.annotation.Aspect"/>

</context:component-scan>

<!-- 启动@AspectJ支持 -->

<aop:aspectj-autoproxy/>

</beans>

测试代码



public class BeanTest

{

public static void main(String[] args)

{

// 创建Spring容器

ApplicationContext ctx = new

ClassPathXmlApplicationContext("beans.xml");

Hello hello = ctx.getBean("hello" , Hello.class);

hello.foo();

hello.addUser("孙悟空" , "7788");

World world = ctx.getBean("world" , World.class);

world.bar();

}

}


来自为知笔记(Wiz)

附件列表

时间: 2024-08-01 05:32:52

【Spring-AOP-学习笔记-5】@AfterReturning增强处理简单示例的相关文章

Spring AOP学习笔记(1)-概念

1.Aspect 横切在多个类的一个关注点,在Spring AOP中,aspect实现是一个规则的类或@Aspect标注的规则类.例如:事务管理 2.Join point 程序执行过程中的一个点,例如:执行一个方法或处理一个异常,在Spring AOP中,一个连接点表示一个方法执行执行 3.Advice 在一个特定的连接点上所采取的动作,类型包括around,before,after等,Spring中 advice就是一个interceptor模式,包括around连接点的interceptor

Spring AOP 学习笔记

1.通过xml配置aop 1.1 新建一个aop的切面类 @Aspect public class MkztAspect { private static final Logger log = LogManager.getLogger(MkztAspect.class); public void mkztAround(JoinPoint point) { System.out.print("开始执行模块状态切面."); MethodInvocationProceedingJoinPoi

Android学习笔记—Windows下NDK开发简单示例

该示例假设Android开发环境已经搭建完成,NDK也配置成功: 1.在Eclipse上新建Android工程,名称为ndkdemo.修改res\layout\activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layo

Spring入门IOC和AOP学习笔记

Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容器中Bean之间的依赖关系,使用一种叫做"依赖注入"的方式来管理bean之间的依赖关系. Spring有两个核心接口:BeanFactory和ApplicationContext,ApplicationContext是BeanFactory的子接口.它们都可以代表Spring容器,Spri

Spring AOP 学习例子

http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example 工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保持进步状态.第二:为了提升技术,提高开发能力.第三:保持程序员对技术和学习的热情,工作的激情.程序员还是需要把基础打扎实,修炼自己的内功.” 所以赶紧把学习的东西总结一下,加深印象.之前有说了下AOP的原理 (http://

Spring MVC 学习笔记(二):@RequestMapping用法详解

一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.xml中配置 <servlet>     <servlet-name>servletName</servlet-name>     <servlet-class>ServletClass</servlet-class> </servlet>

Spring Batch学习笔记二

此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch的架构 一个Batch Job是指一系列有序的Step的集合,它们作为预定义流程的一部分而被执行: Step代表一个自定义的工作单元,它是Job的主要构件块:每一个Step由三部分组成:ItemReader.ItemProcessor.ItemWriter:这三个部分将执行在每一条被处理的记录上,ItemReader读取每一条记录,然后传递给ItemProcessor处理,最后交给ItemWriter做持久化:It

[Spring MVC]学习笔记--DispatcherServlet

在上一篇我们介绍了Servlet,这一篇主要来看一下MVC中用到的DispatcherServlet(继承自HttpServlet). 1. DispatcherServlet在web.xml中被声明. <web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet&l

Spring MVC学习笔记(一)--------准备篇

这一系列笔记将带你一步一步的进入Spring MVC,高手勿喷. 首先你得安装以下的工具: JDK,虽然JDK8已经发布了一段时间了,但是由于我们并不会使用到里面的新特性,所以JDK6以上版本皆可以(需加入到PATH环境变量中): Servlet Container,为了能运行WEB应用程序,因此需要一个Web Container,这里我们建议Tomcat即可: IDE,一个好的IDE不仅能提高你开发的效率,还能降低你学习的成本,我们选择的是IntelliJ: 构建工具,推荐使用Gradle,它