打造第一个Spring AOP的示例

来源:互联网 发布:淘宝卖家买家秀怎么弄 编辑:程序博客网 时间:2024/04/30 11:15

俺是用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技术,敬请关注下我的博客

原创粉丝点击