spring aop 之二 xml方式
前篇介紹的是使用注釋方式實現(xiàn)aop,本篇將介紹使用xml方式實現(xiàn)。
先看xml的配置:
<?xml version="1.0" encoding="UTF-8"?> <bean id="helloService" class="cn.com.ultrapower.service.HelloServiceImpl"> 特殊的是<aop:config>...</aop:config>中的監(jiān)聽器配置。定義了切面,切點,前置方法和后置方法。 然后再看看logAdvisor的代碼: public class LogAdvisor { 很簡單,就兩個普通方法,before用于前置打印信息,after用于后置打印信息。
<beans xmlns="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
</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。另外定義了一個用于監(jiān)聽的logAdvisor。
public void before() {
System.out.println("Log:before method!");
}
public void after() {
System.out.println("Log:after method!");
}
}