计算王:Spring的AOP例子

来源:百度文库 编辑:九乡新闻网 时间:2024/07/07 13:30:05
一个简单的Spring的AOP例子
经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java
 /**
 *
  */
 package  com.dragon.study;

 /**
 *  @author  dragon
 *
  */
10  public   interface  IStudent  {
11 
12      public   void  addStudent(String name);
13 }
14 
目标类:StudentImpl.java
 /**
 *
  */
 package  com.dragon.study.Impl;

 import  com.dragon.study.IStudent;

 /**
 *  @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 /**
 *
  */
 package  com.dragon.Advice;

 import  java.lang.reflect.Method;

 import  org.springframework.aop.MethodBeforeAdvice;

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
2
3
4
5
6
7
8
9
10
11
12  
13    com.dragon.study.IStudent
14  
15  
16    
17     beforeAdvice
18     afterAdvice
19    compareInterceptor
20    

21  
22  
23    
24  
25
26
27
28
29
30
31

现在开始写测试类,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}