Spring AOP使用(基于XML Schema)

  本文主要简单的介绍了如何基于XML Schema方式使用AOP。本文测试使用的是Spring3.1  + AspectJ1.6.

1.IHelloWord.java

package com.SpringAOP.HelloWord;

public interface IHelloWord {
public void sayHello(String message);
}

2.HelloWord.java

package com.SpringAOP.HelloWord;

public class HelloWord implements IHelloWord {
public void sayHello(String message){
System.
out.println(message);
//int i=10/0;
}
}

3.HelloWordAspect.java

package com.SpringAOP.HelloWord;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class HelloWordAspect {
public void beforeSayHello(JoinPoint joinPoint){

System.
out.println("Before :"+joinPoint.getArgs()[0]);
}

public void afterSayHello(String message){
System.
out.println("After : "+message);
}

public void aroundSayHello(ProceedingJoinPoint joinPoint) throws Throwable{
System.
out.println("Around Before !! ");
joinPoint.proceed();
System.
out.println("Around After !! ");
}

public void afterThrowingSayHello(Exception ex){
System.
out.println("After Throwing : "+ex.getMessage());
}


public void afterReturningSayHello(String reval){
System.
out.println("After Returning : "+reval);
}
}

4.HelloWord.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>

<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<bean id="helloWordAspect" class="com.SpringAOP.HelloWord.HelloWordAspect"></bean>
<bean id="helloWord" class="com.SpringAOP.HelloWord.HelloWord"></bean>

<aop:config>
<aop:aspect ref="helloWordAspect">
<aop:before method="beforeSayHello" pointcut="execution(* com.SpringAOP.HelloWord.HelloWord.*(..))"/>
<aop:after-returning method="afterReturningSayHello" pointcut="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..))" returning="reval"/>
<aop:after method="afterSayHello" pointcut="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..)) and args(message)"/>
<aop:after-throwing method="afterThrowingSayHello" pointcut="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..))" throwing="ex"/>
<aop:around method="aroundSayHello" pointcut="execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(..))"/>
</aop:aspect>
</aop:config>
</beans>

5.HelloWordTest.java

package com.SpringAOP.HelloWord;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class HelloWordTest {

@Test
public void test(){
ApplicationContext ctx
=new ClassPathXmlApplicationContext("com/SpringAOP/HelloWord/HelloWord.xml");

IHelloWord helloWord
= (IHelloWord)ctx.getBean("helloWord");
helloWord.sayHello(
"Hello Word!!");
}
}

6.需要导入的包

7.运行结果

作者:红枫落叶
出处:http://www.cnblogs.com/wushiqi54719880/
关于作者:专注于Java企业运用、海量数据处理、hadoop、数字图像处理等。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过wushiqi54719880@126.com  联系我,非常感谢。


posted @ 2011-08-09 22:49  红枫落叶  阅读(789)  评论(2)    收藏  举报