`
meiowei
  • 浏览: 147105 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

一个简单的spring AOP例子

阅读更多

这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java

 

 1   /**
 2  * 
 3   */ 
 4  package  com.dragon.study;
 5  
 6   /** 
 7  *  @author  dragon
 8  *
 9   */ 
10   public   interface  IStudent   {
11     
12      public   void  addStudent(String name);
13 } 

 

目标类:StudentImpl.java

1    /** 
 2  * 
 3   */ 
 4  package  com.dragon.study.Impl;
 5  
 6  import  com.dragon.study.IStudent;
 7  
 8    /** 
 9  *  @author  dragon
10  *
11   */ 
12   public   class  StudentImpl  implements  IStudent  {
13  
14        public   void  addStudent(String name)  {
15          System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );
16      } 
17 } 
18 

 

前置通知:BeforeAdvice.java

1    /** 
 2  * 
 3   */ 
 4  package  com.dragon.Advice;
 5  
 6  import  java.lang.reflect.Method;
 7  
 8  import  org.springframework.aop.MethodBeforeAdvice;
 9  
10    /** 
11  *  @author  dragon
12  *
13   */ 
14   public   class  BeforeAdvice  implements  MethodBeforeAdvice  {
15  
16        public   void  before(Method method,Object[] args, Object target)
17                  throws  Throwable  {
18           
19           System.out.println( " 这是BeforeAdvice类的before方法. " );
20           
21       } 
22 } 

 

后置通知:AfterAdvice.java

1/**
 2 * 
 3 */
 4package com.dragon.Advice;
 5
 6import java.lang.reflect.Method;
 7
 8import org.springframework.aop.AfterReturningAdvice;
 9
10/**
11 * @author dragon
12 *
13 */
14public class AfterAdvice implements AfterReturningAdvice{
15    
16    public void afterReturning(Object returnValue ,Method method,
17                   Object[] args,Object target) throws Throwable{
18        System.out.println("这是AfterAdvice类的afterReturning方法.");
19    }
20      
21
22}

 

环绕通知:CompareInterceptor.java

1/**
 2 * 
 3 */
 4package com.dragon.Advice;
 5
 6import org.aopalliance.intercept.MethodInterceptor;
 7import org.aopalliance.intercept.MethodInvocation;
 8
 9
10/**
11 * @author dragon
12 *
13 */
14public class CompareInterceptor implements MethodInterceptor{
15
16      public Object invoke(MethodInvocation invocation) throws Throwable{
17          Object result = null;
18         String stu_name = invocation.getArguments()[0].toString();
19         if ( stu_name.equals("dragon")){
20             //如果学生是dragon时,执行目标方法,
21              result= invocation.proceed();
22              
23         } else{
24             System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
25         }
26        
27          return result;
28      }
29}

 

配置文件applicationContext.xml

1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3
 4<beans>
 5
 6<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 7<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 8<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
 9<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10
11<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12  <property name="proxyInterfaces">
13    <value>com.dragon.study.IStudent</value>
14  </property>
15  <property name="interceptorNames">
16    <list>
17     <value>beforeAdvice</value>
18     <value>afterAdvice</value>
19    <value>compareInterceptor</value>  
20    </list>
21  </property>
22  <property name="target">
23    <ref bean="studenttarget"/>
24  </property>
25
26</bean>
27
28</beans>

 

现在开始写测试类,Test.java

1/**
 2 * 
 3 */
 4package com;
 5
 6import org.springframework.context.ApplicationContext;
 7import org.springframework.context.support.FileSystemXmlApplicationContext;
 8
 9import com.dragon.study.IStudent;
10
11/**
12 * @author dragon
13 *
14 */
15public class Test {
16
17    /**
18     * @param args
19     */
20    public static void main(String[] args) {
21        // TODO Auto-generated method stub
22      ApplicationContext ctx = 
23          new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
24      
25      IStudent person = (IStudent)ctx.getBean("student");
26      person.addStudent("dragon");
27      
28//      person.addStudent("javadragon");
29    }
30
31}
32

 

当然,运行该程序时,要加上commons-logging.jar 包

 

在Spring Appactiocontext.xml配置文件;你定义的前置,后置;环绕等通知在配置文件中实现了代理(org.springframework.aop.framework.ProxyFactoryBean)
以此将通知放入到了原Bean中;这样才能使原Bean中方法调用时自动执行通知
这是其一》
<property name="proxyInterfaces">
<property name="interceptorNames">
<property name="target">
这三个属性是一定要配置的
第一是被代理的接口(IStudent)
第二是通知列表(前置,后置;环绕)上面定义的三个类
第三是被代理的原Bean(StudentImpl )

分享到:
评论

相关推荐

    简单spring aop 例子

    现在,我们来看如何创建一个简单的Spring AOP例子: 1. **定义切面(Aspect)**:切面是包含通知(Advice)和切入点(Pointcut)的类。通知定义了要执行的逻辑,切入点定义了何时执行。例如,我们可以创建一个名为`...

    Spring AOP完整例子

    在Spring AOP的例子中,我们可能会创建一个`@RunWith(SpringJUnit4ClassRunner.class)`标记的测试类,以利用Spring的测试支持。在测试方法中,可以注入需要的bean,然后调用方法来触发AOP代理。这样,通知将在适当的...

    spring aop例子

    在"spring aop例子"中,我们可以看到`src`目录,这个通常包含Java源代码,其中可能包含了切面类、通知方法以及使用了切面的业务逻辑类。`extjstest`可能是测试目录,用于验证AOP配置和功能是否正确工作。`QQ五笔截图...

    spring的aop简单例子

    这个简单例子将帮助我们理解AOP的基本概念和如何在Spring框架中实现它。 首先,我们要知道AOP的核心概念:切面、通知、连接点、切点、目标对象和代理。切面是包含横切关注点(如日志记录、事务管理等)的模块化组件...

    spring aop简单例子

    现在,我们来看一个简单的Spring AOP例子: 首先,我们需要定义一个切面,包含一个通知。例如,我们创建一个名为`LoggingAspect`的类,其中有一个`logExecutionTime`方法作为前置通知,记录方法的执行时间: ```...

    Spring 2.5 AOP 例子

    Spring 2.5 AOP(面向切面编程)...综上所述,Spring 2.5 AOP例子展示了如何利用类扫描和XML配置实现面向切面编程。通过理解这些概念和实践,我们可以更高效地管理和维护我们的应用程序,使其更具可扩展性和可维护性。

    spring aop 经典例子(原创)

    在`testaop`这个文件夹中,可能包含了一个简单的Spring配置文件(如`applicationContext.xml`),用于定义Bean和AOP配置。在配置文件中,我们需要声明一个切面类,该类包含了我们定义的通知。例如: ```xml &lt;aop:...

    SpringAOP的例子

    在这个"SpringAOP的例子"中,我们将深入探讨如何在Eclipse环境下利用Spring AOP和动态代理来实现这些功能。 首先,让我们理解什么是AOP。AOP是一种编程范式,旨在减少代码的重复性和增强可维护性。在传统的OOP中,...

    Spring AOP例子实现源码

    在`springAop`目录下,你应该会看到如`LoggingAspect.java`这样的源码文件,它实现了切面逻辑。此外,可能还有一个`Main`类用于启动应用程序并演示AOP如何工作。通过运行这个示例,你可以观察到切面是如何在运行时...

    spring aop 注解例子

    本例子就是一个使用 Spring AOP 注解实现的项目,旨在展示如何在简单的环境中应用这一特性。 1. **AOP 基础概念** - **切面(Aspect)**: 包含一组相关功能的模块,这些功能在多个对象中都可能被用到,比如日志、...

    springAOP演示例子

    这个"springAOP演示例子"很可能会包含一个简单的Spring项目,展示如何创建和配置切面,定义切入点和通知,并观察其在实际代码中的工作原理。通过深入理解和实践这个例子,你可以更好地掌握Spring AOP的使用,提升你...

    spring AOP 例子参考

    Spring AOP,全称为Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,它为Java应用程序提供了声明式事务管理、日志记录、性能监控等跨切面关注点的解决方案。本教程将深入探讨Spring AOP...

    SpringAOP例子

    在这个"SpringAOP例子"中,我们将探讨Spring AOP如何实现以及如何在实际项目中应用。 首先,Spring AOP通过代理模式实现了面向切面编程。它提供了两种类型的代理:JDK动态代理和CGLIB代理。JDK动态代理基于接口,当...

    spring mvc框架下的aop例子

    下面是一个简单的Spring MVC中AOP的例子,分为配置和实现两部分。 ### 配置 1. 在`spring-common.xml`配置文件中,我们需要引入AOP相关的命名空间,并声明一个`&lt;aop:aspectj-autoproxy&gt;`元素。这会告诉Spring容器...

    Spring.net Aop 例子

    Spring.NET会创建一个实现了这些接口的代理类,当调用接口方法时,代理会先执行预定义的切面(Advice),然后转发调用给实际的目标对象。这种方式简单且高效,但仅限于接口实现。 2. **基于类型(TypeProxy)**:...

    Spring AOP简单demo

    Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个重要模块,它扩展了传统的面向对象编程,允许开发者定义“横切关注点”(cross-cutting concerns),如日志、事务管理、性能监控等。...

    spring aop的demo

    在`springAop1`这个压缩包中,可能包含了一个简单的应用示例,展示了如何定义一个切面类,以及如何在该类中定义通知方法。例如,我们可能会看到一个名为`LoggingAspect`的类,其中包含了`@Before`注解的方法,用于在...

    Spring AOP 的实现例子(基于XML配置实现)

    Spring AOP,即Spring的面向切面编程,是Spring框架中的一个重要组成部分,它提供了一种在不修改原有代码的情况下,对程序进行功能增强的技术。在实际开发中,我们经常使用AOP来处理如日志记录、事务管理、权限校验...

    spring aop spring aop

    Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,用于实现横切关注点的模块化。它允许开发者定义“切面”,将那些与业务逻辑无关,却为多个对象共有的行为(如日志、...

    Spring AOP 日志管理 实例

    Spring AOP 日志管理 实例LoggingThrowsAdvice.java

Global site tag (gtag.js) - Google Analytics