spring使用动态代理面向切面编程(AOP) xml
在使用注解的时候,首先得在配置文件bean.xml中添加命名空间:
xmlns:aop="http://www.springframework.org/schema/aop"
然后在xsi:schemaLocation中添加:
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
bean.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="com.test"></context:component-scan>
<bean id="myInterceptor" class="com.test.aop.MyInterceptor"></bean>
<bean id="myInterceptor2" class="com.test.aop.MyInterceptor2"></bean>
<bean id="myInterceptor3" class="com.test.aop.MyInterceptor3"></bean>
<aop:config>
<aop:aspect ref="myInterceptor">
<aop:pointcut id="myMethod" expression="execution(public void com.test.dao.impl.UserDAOImpl.save(com.test.model.User))"/>
<aop:before method="beforeMethod" pointcut-ref="myMethod"/>
</aop:aspect>
<aop:aspect ref="myInterceptor2">
<aop:pointcut id="myMethod2" expression="execution(public * com.test.dao..*.*(..))"/>
<aop:before method="beforeMethod" pointcut-ref="myMethod2"/>
<aop:after-returning method="afterReturningMethod" pointcut-ref="myMethod2"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="myMethod2"/>
<aop:around method="aroundMethod" pointcut-ref="myMethod2"/>
</aop:aspect>
<aop:aspect ref="myInterceptor3">
<aop:pointcut id="myMethod3" expression="execution(public * com.test.service..*.add(..))"/>
<aop:before method="beforeMethod" pointcut-ref="myMethod3"/>
<aop:after-returning method="afterReturningMethod" pointcut-ref="myMethod3"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="myMethod3"/>
<aop:around method="aroundMethod" pointcut-ref="myMethod3"/>
</aop:aspect>
</aop:config>
</beans>
MyInterceptor.java
package com.test.aop;
public class MyInterceptor {
public void beforeMethod() {
System.out.println("start mothod!");
}
}
MyInterceptor2.java
package com.test.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyInterceptor2 {
public void beforeMethod() {
System.out.println("MyInterceptor2 before mothod!");
}
public void afterReturningMethod() {
System.out.println("MyInterceptor2 afterReturning mothod!");
}
public void afterThrowingMethod() {
System.out.println("MyInterceptor2 afterThrowing mothod!");
}
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("MyInterceptor2 around start mothod!");
pjp.proceed();
System.out.println("MyInterceptor2 around end mothod!");
}
}
MyInterceptor3.java
package com.test.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyInterceptor3 {
public void beforeMethod() {
System.out.println("MyInterceptor3 before mothod!");
}
public void afterReturningMethod() {
System.out.println("MyInterceptor3 afterReturning mothod!");
}
public void afterThrowingMethod() {
System.out.println("MyInterceptor3 afterThrowing mothod!");
}
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("MyInterceptor3 around start mothod!");
pjp.proceed();
System.out.println("MyInterceptor3 around end mothod!");
}
}