spring AOP的Around增强实现方法分析
作者:cakincqm 发布时间:2021-10-03 06:22:39
标签:spring,AOP
本文实例讲述了spring AOP的Around增强实现方法。分享给大家供大家参考,具体如下:
一 配置
<?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>
二 切面类
package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定义一个切面
@Aspect
public class TxAspect
{
// 匹配org.crazyit.app.service.impl包下所有类的、
// 所有方法的执行作为切入点
@Around("execution(* org.crazyit.app.service.impl.*.*(..))")
public Object processTx(ProceedingJoinPoint jp)
throws java.lang.Throwable
{
System.out.println("执行目标方法之前,模拟开始事务...");
// 获取目标方法原始的调用参数
Object[] args = jp.getArgs();
if(args != null && args.length > 1)
{
// 修改目标方法的第一个参数
args[0] = "【增加的前缀】" + args[0];
}
// 以改变后的参数去执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(args);
System.out.println("执行目标方法之后,模拟结束事务...");
// 如果rvt的类型是Integer,将rvt改为它的平方
if(rvt != null && rvt instanceof Integer)
rvt = (Integer)rvt * (Integer)rvt;
return rvt;
}
}
三 接口
Hello
package org.crazyit.app.service;
public interface Hello {
// 定义一个简单方法,模拟应用中的业务逻辑方法
void foo();
// 定义一个addUser()方法,模拟应用中的添加用户的方法
int addUser(String name, String pass);
}
World
package org.crazyit.app.service;
public interface World {
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void bar();
}
四 实现类
HelloImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@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;
}
}
WorldImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("world")
public class WorldImpl implements World {
// 定义一个简单方法,模拟应用中的业务逻辑方法
public void bar() {
System.out.println("执行World组件的bar()方法");
}
}
五 测试类
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
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();
}
}
六 测试结果
执行目标方法之前,模拟开始事务...
执行Hello组件的foo()方法
执行目标方法之后,模拟结束事务...
执行目标方法之前,模拟开始事务...
执行Hello组件的addUser添加用户:【增加的前缀】孙悟空
执行目标方法之后,模拟结束事务...
addUser()的返回值为:400
执行目标方法之前,模拟开始事务...
执行World组件的bar()方法
执行目标方法之后,模拟结束事务...
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101560567


猜你喜欢
- 前言:Guarded Suspension意为保护暂停,其核心思想是仅当服务进程准备好时,才提供服务。设想一种场景,服务器可能会在很短时间内
- MapperScan添加动态配置(占位符)在对Mybatis自动扫描配置中,使用注解配置时,@MapperScan中的配置,通常配置如下:@
- 在很多的Android项目中都需要用户登录、注册。这样的话在开发中做好保护用户密码的工作就显得尤为重要。这里我把自己的密码保护方法记录下来。
- 1、作用域1.1 作用域的作用作用域——scope通常来说,一段程序代码中所用到的名字并不总是有效/
- 今天闲来无事写了一个清内存的小东西,类似360,在桌面上悬浮,点击后清除后台无用程序,清除后台程序是通过调用ActivityManger.k
- 今天聊一个小伙伴在星球上的提问:问题不难,解决方案也有很多,因此我决定撸一篇文章和大家仔细说说这个问题。1. 配置文件位置首先小伙伴们要明白
- 脚本之家在以前介绍过关于C#创建、部署、调用WebService的教程,有兴趣的可以参阅:.NET C#创建WebService服务简单实例
- 本文实例为大家分享了使用C#写一个时钟,供大家参考,具体内容如下时钟是这样的一共使用四个控件即可:WinFrom窗体应用程序代码:using
- 0.解释器(Interpreter)模式定义 :给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中句子。
- 1. 用indexof的方法:public class Test11 {private static int counter = 0;/**
- 一、首先来看一个例子package net.println.kotlin.chapter4/** * @author:wangdong *
- 本文为大家分享了如何使用eclipse创建java项目,供大家参考,具体内容如下首先,打开Eclipse,在工具栏依次点击【File】>
- 前言一个简单的单机小游戏:flypybird ,用来巩固java基础。涉及主要知识点:JFrame 、 JPanel 、 继承、 键盘/鼠标
- 1. 前言Compose 具有超强的兼容性,兼容现有的所有代码,Compose 能够与现有 View 体系并存,可实现渐进式替换。这就很有意
- 组件在容器(比如Jframe)中的位置和大小是由布局管理器来决定的。所有的容器都会使用一个布局管理器,通过它来自动进行组件的布局管理。种类j
- 一、为何使用内部类内部类提供了更好的封装,只有外部类能访问内部类内部类可以独立继承一个接口,不受外部类是否继承接口影响内部类中的属性和方法即
- 使用stream判断两个list元素的属性并输出/*** 使用stream判断两个list中元素不同的item*/@Testpublic v
- 通过2种方式模拟单个文件上传,效果如下所示开发步骤如下:1、新建一个web工程,导入struts2上传文件所需jar,如下图目录结构2、新建
- 前言提起子类、基类和方法继承这些概念,肯定大家都非常熟悉。毕竟,作为一门支持OOP的语言,掌握子类、基类是学习C#的基础。不过,这些概念虽然
- 进程间图怎么传递图形buffer写这篇文章的目的:讲解 进程间图怎么传递图形buffer的最近研究图形缓存怎么在进程之间传递的,谷歌了所有的