我爱熊猫

最新评论

spring aop 之二 xml方式

前篇介绍的是使用注释方式实现aop,本篇将介绍使用xml方式实现。

先看xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="helloService" class="cn.com.ultrapower.service.HelloServiceImpl">
</bean>
<bean id="helloAction" class="cn.com.ultrapower.action.HelloActionImpl">
<property name="helloService"><ref bean="helloService"/></property>
<property name="name" value="yoo"></property>
</bean>
<bean id="logAdvisor" class="cn.com.ultrapower.advice.schemabased.LogAdvisor">
</bean>
<aop:config>
<aop:aspect ref="logAdvisor">
<aop:pointcut id="someMethod" expression="execution(* cn.com.ultrapower.action.IHelloAction.*(..))"/>
<aop:after-returning pointcut-ref="someMethod" method="after"/>
<aop:before pointcut-ref="someMethod" method="after"/>
</aop:aspect>
</aop:config>
</beans>
这里定义了两个普通的bean:helloService和helloAction。另外定义了一个用于监听的logAdvisor。

特殊的是<aop:config>...</aop:config>中的监听器配置。定义了切面,切点,前置方法和后置方法。

然后再看看logAdvisor的代码:

public class LogAdvisor {

public void before() {
System.out.println("Log:before method!");
}

public void after() {
System.out.println("Log:after method!");
}
}

很简单,就两个普通方法,before用于前置打印信息,after用于后置打印信息。

posted on 2008-06-07 20:54 flyoo 阅读(79) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问