转载自: http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html

经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵呵。现在写的这个只是个简单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 
14 

目标类: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 
23 

后置通知: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}
23

环绕通知: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}
30

配置文件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
29
30
31</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

一个简单的Spring AOP例子的更多相关文章

  1. 一个简单的Spring测试的例子

    在做测试的时候我们用到Junit Case,当我们的项目中使用了Sring的时候,我们应该怎么使用spring容器去管理我的测试用例呢?现在我们用一个简单的例子来展示这个过程. 1 首先我们新建一个普 ...

  2. 在eclipse中配置一个简单的spring入门项目

    spring是一个很优秀的基于Java的轻量级开源框架,为了解决企业级应用的复杂性而创建的,spring不仅可用于服务器端开发,从简单性.可测试性和松耦合性的角度,任何java应用程序都可以利用这个思 ...

  3. 创建一个简单的Spring应用

    环境已经安装完成,接下来创建一个简单的Spring应用. 创建Spring应用步骤: 创建一个maven项目 添加spring库依赖 创建Bean类 添加Bean的xml装配文件 创建主类 运行应用程 ...

  4. [转]一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程

    一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程 希望此文能给初学多线程编程的朋友带来帮助,也希望牛人多多指出错误. 另外感谢以下链接的作者给予,给我的学习带来了很大帮助 http ...

  5. 一个简单的iBatis入门例子

    一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...

  6. 2.1 一个简单的Web工程例子

    一个简单的Web工程例子 开发环境: Eclipse: Neon Release (4.6.0) JDK:1.8.0_92 Tomcat:8.5.9 Maven:3.3.9 1. 在Eclipse中创 ...

  7. 构建一个简单的Spring Boot项目

    11 构建一个简单的Spring Boot项目 这个章节描述如何通过Spring Boot构建一个"Hello Word"web应用,侧重介绍Spring Boot的一些重要功能. ...

  8. 一个简单的spring boot程序

    搭建一个spring boot项目十分的方便,网上也有许多,可以参考 https://www.cnblogs.com/ityouknow/p/5662753.html 进行项目的搭建.在此我就不详细介 ...

  9. JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)

    接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...

随机推荐

  1. android 百度地图定位功能实现

    历经几天时间,终于把定位功能给实现了,可谓是费劲千辛万苦啊,有定位知识还有图层知识,在这里我把代码给大家贴出来,一起分享一下下啦. package com.example.foreveross.off ...

  2. 四、cocos2dx动画Animation介绍

    qinning199原创,欢迎转载,转载请注明:http://www.cocos2dx.net/?p=22 一.帧动画 你可以通过一系列图片文件,像如下这样,创建一个动画: CCAnimation * ...

  3. C++ 矩阵乘法

    //构造矩阵类,重载乘法操作符 //作者:nuaazdh //时间:2011年12月1日 #include <iostream> using namespace std; //Matrix ...

  4. 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit

    this bundle is invalid . new apps and app updates submitted to the app store must be built with publ ...

  5. nutch2.3命令参数解析

    nutch中可执行的命令列表 [root@ewanalysis ~]# nutch Usage: nutch COMMAND where COMMAND is one of: inject injec ...

  6. mac nodejs&npm 安装

    https://www.baidu.com/link?url=Ekv7EzWuMOXjIqFL_ewddWzdahU7jMAsWY4gOGOjMtC&ie=UTF-8&wd=nodej ...

  7. zen-Coding的使用

    zen-Coding的使用 zen-Coding的使用需要掌握CSS和HTML相关知识.其实只要对CSS的选择器比较熟悉,就可以得用简短的类似于CSS选择器的代码高效的编写出HTML代码.打开Note ...

  8. struts2中使用json插件实现ajax交互

    json插件可以简单的实现ajax交互,避免了使用struts2-dojo-plugin.jar包时带来的struts2.x版本冲突问题.并且减少了使用ajax标签时需要的繁琐的配置包括web.xml ...

  9. AutoCompleteTextview、MultiAutoCompleteTextView

    AutoCompleteTextview     动态匹配输入内容文本框      属性:           android:completionThreshold="2";   ...

  10. Docker镜像与仓库(一)

    Docker镜像与仓库(一) Docker镜像与仓库(一) 如何查找镜像? Docker Hub https://registry.hub.docker.com docker search [OPTI ...