打造第一个Spring AOP的示例

俺是用Eclipse作的,看过之后做做看,体验一下AOP是什么东东阿!好,不多废话了,让我们开始吧.

                                                      第一步 建立一个接口

 

package  com.neusoft.impl;

public   interface  IBusinessLogic {
    
public   void  foo () ;

}

 

第二步 建立接口的实现类

 

package  com.neusoft.action;

import  com.neusoft.impl.IBusinessLogic;

public   class  BusinessLogic  implements  IBusinessLogic {
public   void  foo() {
    System.out.println(
" Inside BusinessLogic.foo() " );
}
}

 

第三步 建立Before通知

 

package  com.neusoft.action;

import  java.lang.reflect.Method ;
import  org.springframework.aop.MethodBeforeAdvice ;

public   class  TracingBeforeAdvice  implements  MethodBeforeAdvice {
    
public   void  before ( Method m, Object[] args, Object target )  throws  Throwable {
        System.out.println(
" Hello lisi!(by "    +   this .getClass().getName()  +   " ) " ) ;
        
    }
}

 

第四步 建立After通知

 

package  com.neusoft.action;
import  java.lang.reflect.Method;
import  org.springframework.aop.AfterReturningAdvice ;
public   class  TracingAfterAdvice  implements  AfterReturningAdvice {
public   void  afterReturning ( Object object, Method m, Object[] args, Object target ) 
throws  Throwable {
    System.out.println (
" Hello lisi!(by "   +   this .getClass().getName()  +   " ) " ) ;
}
}

 

第五步 建立配置文件(springconfig.xml)

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>
< beans >
<!--  Bean configuration  -->
< bean  id  = "businesslogicbean"
class 
= "org.springframework.aop.framework.ProxyFactoryBean" >
< property  name  = "proxyInterfaces" >
< value > com.neusoft.impl.IBusinessLogic </ value >

</ property >
< property  name ="target" >
< ref  local  = "beanTarget" />
</ property >
</ bean >
<!--  Bean Classes  -->
< bean  id  = "beanTarget"
class 
= "com.neusoft.action.BusinessLogic" >
</ bean >

<!--  Advice classes  -->

< bean  id  = "theTracingBeforeAdvice"
class 
= "com.neusoft.action.TracingBeforeAdvice" >
</ bean >
< bean  id  = "theTracingAfterAdvice"
class 
= "com.neusoft.action.TracingAfterAdvice" >
</ bean >

<!--  Advisor pointcut definition for before advice  -->

< bean  id  = "theTracingBeforeAdvisor"
class 
= "org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
< property  name  = "advice" >
< ref  local  = "theTracingBeforeAdvice" />
</ property >
< property  name ="patterns" >
< value > .* </ value >
</ property >
</ bean >

<!--  Advisor pointcut definition for after advice  -->

< bean  id  = "theTracingAfterAdvisor"
class 
= "org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
< property  name  = "advice" >
< ref  local  = "theTracingAfterAdvice" />
</ property >
< property  name  = "patterns" >
< value > .* </ value >
</ property >
</ bean >
</ beans >

 

第六步 建立测试类

 

package  com.neusoft.test;

import  org.springframework.context.ApplicationContext ;
import  org.springframework.context.support.FileSystemXmlApplicationContext;

import  com.neusoft.impl.IBusinessLogic;

public   class  MainApplication {

    
/**
     * 
@author  lishijin
     
*/
    
public   static   void  main(String[] args) {

        
// Read the configration file
        ApplicationContext ctx  =   new  FileSystemXmlApplicationContext( " /WEB-INF/springconfig.xml " ) ;
        
// Instantiate an object
        IBusinessLogic testObject  =  (IBusinessLogic)ctx.getBean( " businesslogicbean " );
        
// Execute the public
        
// method of the bean
        testObject.foo() ;
    }

}

 

运行结果:

其中我没有做出详细的解释,我想你动手做之后,自然会知道是怎么回事,最近在研究AOP技术,敬请关注下我的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值