解释就懒得写了,有代码运行一下就知道了,jar除了Spring所必备的以外还要加上bsh-1.2b7.jar
-
packagecom.test;
-
-
-
publicinterfaceBookBiz{
-
publicfloat[]buy(StringuserName,StringbookName,doubleprice);
-
publicvoidcomment(StringuserName,Stringcomments);
- }
-
packagecom.test;
-
publicclassBookBizImplimplementsBookBiz{
-
-
-
publicfloat[]buy(StringuserName,StringbookName,doubleprice){
-
System.out.println("业务方法buy开始执行");
-
System.out.println("·"+userName+"购买图书:"+bookName);
-
System.out.println("·"+userName+"增加积分:"+(int)(price/10));
-
System.out.println("·"+"向物流系统下发货单");
-
System.out.println("业务方法buy结束");
-
returnnull;
- }
-
-
-
publicvoidcomment(StringuserName,Stringcomments){
-
System.out.println("业务方法comment开始执行");
-
System.out.println("·"+userName+"发表书评"+comments);
-
System.out.println("业务方法comment结束");
- }
- }
-
packagecom.test;
-
importjava.lang.reflect.Method;
-
importjava.util.Arrays;
-
importorg.aopalliance.intercept.MethodInterceptor;
-
importorg.aopalliance.intercept.MethodInvocation;
-
importorg.springframework.aop.AfterReturningAdvice;
-
importorg.springframework.aop.MethodBeforeAdvice;
-
importbsh.Interpreter;
-
publicclassMyAdviceimplementsMethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{
-
-
-
publicvoidbefore(Methodm,Object[]args,Objecttarget)
-
throwsThrowable{
-
System.out.println("前通知,调用的方法:"+m.getName()+",参数:"+Arrays.toString(args));
- }
-
-
-
publicvoidafterReturning(Objectonject,Methodmethod,Object[]args,
-
Objectarg3)throwsThrowable{
-
System.out.println("后通知,调用的方法:"+method.getName()+",参数:"+Arrays.toString(args));
- }
-
-
-
publicObjectinvoke(MethodInvocationmethod)throwsThrowable{
-
System.out.println("[环绕通知]");
- Object[]args=method.getArguments();
-
-
-
if(method.getMethod().getName().equals("comment")&"李四".equals(args[0])){
-
System.out.println("屏蔽李四所有的评论");
- StringreturnType=method.getMethod().getReturnType().getName();
-
if("int".equals(returnType)||"long".equals(returnType)||"float".equals(returnType)||"double".equals(returnType)||"byte".equals(returnType)||"short".equals(returnType)){
-
-
-
Interpreteri=newInterpreter();
-
returni.eval("("+returnType+")0");
-
}elseif("boolean".equals(returnType)){
-
returnfalse;
- }
-
returnnull;
-
}else{
-
returnmethod.proceed();
- }
- }
- }
- packagecom.test;
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
-
publicclassAOPTest{
-
-
-
publicstaticvoidmain(String[]args){
- ApplicationContextcontext=
-
newClassPathXmlApplicationContext("springAop.xml");
-
BookBizbookBiz=(BookBiz)context.getBean("bookBiz");
-
bookBiz.buy("张三","Spring深入潜出",50);
-
bookBiz.comment("李四","《恐怖世界》一点都不恐怖,很好看!");
-
bookBiz.comment("张三","《Spring深入潜出》还是写得不错的!");
- }
- }
-
<?xmlversion="1.0"encoding="UTF-8"?>
-
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
-
<beans>
-
<beanid="bookBizTarget"class="com.test.BookBizImpl"/>
-
<beanid="myAdvice"class="com.test.MyAdvice"/>
-
<beanid="bookBiz"class="org.springframework.aop.framework.ProxyFactoryBean">
-
<propertyname="proxyInterfaces">
-
<value>com.test.BookBiz</value>
-
</property>
-
<propertyname="interceptorNames">
-
<list>
-
<value>myAdvice</value>
-
</list>
-
</property>
-
<propertyname="target"ref="bookBizTarget"/>
-
</bean>
-
</beans>
运行AOPTest 这个类,下面是输出结果:
[环绕通知]
前通知,调用的方法:buy,参数:[张三, Spring深入潜出, 50.0]
业务方法buy开始执行
·张三购买图书:Spring深入潜出
·张三增加积分:5
·向物流系统下发货单
业务方法buy结束
后通知,调用的方法:buy,参数:[张三, Spring深入潜出, 50.0]
[环绕通知]
屏蔽李四所有的评论
[环绕通知]
前通知,调用的方法:comment,参数:[张三, 《Spring深入潜出》还是写得不错的!]
业务方法comment开始执行
·张三发表书评《Spring深入潜出》还是写得不错的!
业务方法comment结束
后通知,调用的方法:comment,参数:[张三, 《Spring深入潜出》还是写得不错的!]
从输出结果可以看出,环绕通知是最先开始执行的,,如果环绕通知把目标方法屏蔽了不执行,那么后面的前后通知都不会执行
分享到:
相关推荐
`SpringAop.ppt`文件很可能包含了一个详细的讲解,涵盖了Spring AOP的基本概念、配置方式、使用注解声明切面、基于XML的配置以及如何自定义切面。PPT通常会通过图表、代码示例和流程图来帮助理解复杂的概念,使得...
这个“Spring AOP示例”包含了一个具体的实践案例,帮助我们更好地理解和应用Spring AOP。 在Spring AOP中,核心概念有以下几个: 1. **切面(Aspect)**:切面是关注点的模块化,比如事务管理就是一个切面。在...
以下是一个简单的Spring AOP示例,展示如何使用注解定义切面和通知: ```java // 定义切面 @Aspect @Component public class LoggingAspect { // 定义切入点,匹配所有以'execute'开头的方法 @Pointcut(...
本示例将深入探讨Spring AOP的基础知识,以及如何在实际应用中使用它。 首先,我们来看"LogProfilter.java",这很可能是实现一个日志拦截器的类。在Spring AOP中,这样的类通常被称为切面(Aspect)。切面是封装了...
在这个"spring aop示例"中,我们看到了如何使用Spring AOP来实现方法执行前打印方法名和参数的功能。这主要涉及到三个方面:AOP的基本概念、注解的使用以及Spring的自动注入。 首先,AOP的核心概念包括切面(Aspect...
Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许开发者在不修改源代码的情况下,通过插入切面来增强或改变程序的行为。在本教程中,我们将深入探讨Spring AOP的不同使用方法,包括定义切点、通知类型...
SpringAOP Spring AOP(面向方面的编程)用于模块化“横截面”服务。 用一种简单的方式,我们可以说它是一个旨在拦截某些进程的组件,例如,在执行某个方法时,Spring AOP可以审核该执行方法,并在该方法执行...
在这个“Spring AOP 1.0示例”中,我们重点关注如何在实际项目中应用这一特性。 首先,我们需要了解AOP的基本概念。AOP的核心思想是将那些影响多个类的公共行为(如日志记录)抽取出来,形成独立的模块,称为切面...
接下来,我们通过一个简单的Spring AOP示例来加深对上述概念的理解。假设我们需要在调用某个公共方法前记录日志,我们可以定义一个`BeforeAdvice`,并在目标方法上应用此通知。 ```java package com.example.aop; ...
7. **实战应用**:在提供的示例中,`spring-aop-demo`项目可能包含了以下内容: - `pom.xml`:Maven配置文件,确保依赖了Spring AOP和其他必要的库。 - `src/main/java`:包含切面类、业务类和其他组件的Java源...
在这个"spring aop API示例"中,我们将深入探讨如何利用Spring AOP的四种通知类型:Before、After、AfterThrowing和Around,以及它们在实际开发中的应用。 1. **Before通知**: 在方法执行前触发,可以用来执行...
压缩包中的"aop"文件可能包含了一个简单的Spring AOP示例项目,包括了上述两种实现方式的源代码和配置文件。下载后,可以直接运行以观察AOP如何工作。 总结来说,Spring AOP提供了一种强大的方式来实现横切关注点,...
**Spring AOP示例** 以入门级的`advice`为例,我们可能有一个简单的日志切面: ```java @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.service.*.*(..))") public void ...
项目的源码包含了一个简单的Spring AOP示例,可能包括以下几个部分: 1. `Aspect`类:包含了切面逻辑的类,通常有多个通知方法。 2. `Service`类:业务逻辑类,切面将会影响到的方法。 3. `Spring配置文件`:定义了...
在提供的压缩包文件中,"www.pudn.com.txt"可能是下载来源的说明或者包含一些额外的信息,而"springAop"可能是一个包含Spring AOP示例代码的文件。对于学习和理解Spring AOP以及CGLIB的用法,分析这个文件的内容将...
本示例将简要介绍如何在Spring应用中实现AOP,通过实际的代码示例帮助理解其工作原理。 首先,我们要理解AOP的核心概念。AOP是一种编程范式,它允许开发者定义“切面”(Aspects),这些切面封装了特定的关注点,如...
**使用TestNG运行Spring AOP示例** 在给定的"SpringAOPExample"中,使用`mvn clean test`命令来运行项目。Maven是一个流行的项目管理和依赖管理工具,它会执行clean生命周期阶段以删除已生成的目标文件,然后执行...
package com.ascenttech.springaop.test; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class TestBeforeAdvice implements MethodBeforeAdvice { public void ...
接下来,我们将使用XML配置来创建一个简单的Spring AOP示例: 1. **配置Spring容器**:首先,我们需要一个Spring配置文件(如`springstudy02/spring-context.xml`),在其中定义bean并启用AOP支持。例如: ```xml ...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要组成部分。它提供了一种模块化和声明式的方式来处理系统中的交叉关注点,如日志、性能监控、安全性、事务管理等。通过AOP,...