导包:
- spring-aop-3.2.0.RELEASE.jar
- com.springsource.org.aopalliance-1.0.0.jar
- 还有之前的4+1
切面类
package cn.qyc.banZD;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAspect implements MethodInterceptor{
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("前");
Object object = methodInvocation.proceed();
System.out.println("后");
return object;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
<bean id="userService" class="cn.qyc.banZD.UserServiceImp"></bean>
<bean id="myAspect" class="cn.qyc.banZD.MyAspect"></bean>
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="cn.qyc.banZD.UserService"/>
<property name="target" ref="userService"/>
<property name="interceptorNames" value="myAspect"/>
</bean>
</beans>
前
做饭
后
---------
前
吃饭
后
---------
前
上厕所
后