春天.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="debugInterceptor" class="test.DebugInterceptor" />
<bean id="testBean" class="test.TestBean" />
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*" />
<property name="interceptorNames">
<list>
<value>debugInterceptor</value>
</list>
</property>
</bean>
</beans>
测试豆
package test;
public class TestBean {
public void foo() {
System.out.println("foo");
}
void bar() {
System.out.println("bar");
}
}
调试拦截器
package test;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class DebugInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Before: invocation=[" + invocation + "]");
Object rval = invocation.proceed();
System.out.println("Invocation returned");
return rval;
}
}
测试
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:spring.xml");
TestBean bean = (TestBean)ap.getBean("testBean");
bean.foo();
bean.bar();
}
}