Spring AOP 样例

本文介绍了一个使用 Spring AOP 的示例应用,展示了如何通过配置 XML 文件实现 AOP 的功能,并详细解释了 AspectJ 注解的使用方法,包括前置、后置、返回后、抛出异常后及环绕通知。

Spring AOP 样例  

2011-03-11 18:57:52|  分类: Spring |  标签:spring  aop  样例   |字号 订阅

applicationContext.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
              <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
             <bean id="myAspect" class="com.demo.spring.aop.MyAspect" />
             <bean id="helloWorld" class="com.demo.spring.test.HelloWorld"/>
</beans>

声明一个切面类 :

package com.demo.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {

       @Pointcut("execution(* execute(. .)) && target(com.demo.spring.test.HelloWorld)" )   //切入点函数
        public void pointcut(){
        }


       @Before("pointcut()")  //前置通知 (Before Advice)
        public void beforeExecute(JoinPoint thisJoinPoint){

                       System.out.println("连接点类型:" + thisJoinPoint.getKind());
                       System.out.println("代理类名:" + thisJoinPoint.getSourceLocation().getClass().getName());
                       System.out.println("目标类名:" + thisJoinPoint.getTarget().getClass().getName());
                       System.out.println("目标函数名:" + thisJoinPoint.getSignature().getName());
                       System.out.println("参数个数:" + thisJoinPoint.getArgs().length);

                       System.out.println("Before Advice!");
        }


        @AfterReturning(pointcut = "pointcut()", returning = "retVal")  //返回后通知(After returning Advice)
        public void afterExecuet(Object retVal){
                     System.out.println("返回值为:" + retVal);
        }


        @AfterThrowing(pointcut = "pointcut()", throwing = "ex")  //抛出异常后通知(After Throwing Advice)
        public void afterThrowing(Exception ex){
                    System.out.println("Throwing "+ex);
        }


        @After("pointcut() && args(arg, . .)")  //后通知 (After Advice)
        public void afterExecute(Obecjt arg){

                   System.out.print("参数值为:" + args);
                   System.out.print("After Advice!");
        }


        @Around("pointcut()")   //环绕通知(Around Advice)
        public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{
                   System.out.println("Around Advice!");
                    return thisJoinPoint.proceed();
         }
}

定义一个目标函数:

package com.demo.spring.test;

import org.springframework.context.ApplicationContext;

public class HelloWorld {

          public String execute(String message){
                 System.out.println("Hello " + message + "!");

                 return "Goodbye " + message;
           }

           public static void main(String[] args){  
                 //ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
                 ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
                 HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
                 helloWorld.execute("World");
            }
}

【源码免费下载链接】:https://renmaiwang.cn/s/qqeui ### Python通过Matplotlib生成复合饼图在数据分析与可视化领域,图表是一种强大的工具,能够帮助我们更好地理解数据、发现模式并做出决策。其中,饼图因其直观性而在展示部分与整体的关系时尤为常见。然而,在某些情况下,单一的饼图可能无法完全展现复杂的数据结构或层级关系。这时,复合饼图(也称为嵌套饼图或多层饼图)就派上了用场。#### 复合饼图的概念复合饼图是一种特殊的饼图形式,它将一个或多个较小的饼图嵌入到一个较大的饼图中,以此来表示数据的不同层次或类别。这种图表非常适合用来展示多层次的数据结构,比如市场份额中的细分市场占比等。#### Matplotlib简介Matplotlib是一个用于Python的2D绘图库,它可以生成各种类型的图表,包括线图、柱状图、散点图、饼图等。由于其高度的自定义能力和灵活性,Matplotlib成为了数据科学家和工程师们进行数据可视化的主要工具之一。#### 使用Matplotlib绘制复合饼图下面我们将详细介绍如何使用Matplotlib库在Python中生成复合饼图。1. **导入必要的库** ```python import matplotlib.pyplot as plt from matplotlib.patches import ConnectionPatch ```2. **创建画布和子图** 在复合饼图中,通常会有一个主饼图和一个或多个子饼图。因此,我们需要创建一个包含这些子图的画布。 ```python fig = plt.figure(figsize=(9, 5.0625)) ax1 = fig.add_subplot(121) # 主饼图所在的子图 ax2 = fig.add_subplot(122) # 子饼图所在的子图 fig.
【源码免费下载链接】:https://renmaiwang.cn/s/u0npk 在Java程序设计语言中,将字符串内的字符按字母顺序重新排列是一种常见的操作,在处理文本数据或需要对字母顺序进行排序的场景中尤为常见。本节内容将详细讲解如何实现这一功能。由于Java字符串结构固定,无法对其进行后续更改,因此在对字符串中的字符进行排序时必须采取特殊方式。为达到排序目的,我们需要理解以下关键点:首先,Java字符串是不可变对象,默认由`String`类创建;其次,在这种类型下无法直接修改原有内容,因此实现字符排序需要通过构造新的字符串对象来完成。 具体步骤如下: 1. **转换字符数组**:利用`toCharArray()`方法将原始字符串转换为可操作的字符数组。 2. **排序字符数组**:调用`Arrays.sort()`方法对上述生成的字符数组进行排序,默认按Unicode值排列,对于a-z范围内的字母顺序与Unicode排序一致。 3. **构造新字符串**:通过`new String(charArray)`或`String.valueOf(charArray)`将已排序的字符数组转换为新的字符串对象。 以下示代码展示了这一操作的具体实现: ```javaimport java.util.Arrays;public class Zifuchuan { public static void main(String[] args) { String originalStr = "java 字符串a-z排序"; // 步骤1:将字符串转换为字符数组 char[] chars = originalStr.toCharArray(); // 步骤2:对字符数组进行排序 Arrays.sort(chars); // 步骤3:构建新的排序后的字符串 String sortedStr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值