请先看代码
package aop;
public interface SomeService {
void someMethod();
void someInnerMethod();
}
package aop;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
public class SomeServiceImpl implements SomeService {
private static final Logger log = Logger.getLogger(SomeServiceImpl.class);
protected static ApplicationContext ctx;
public void someMethod() {
someInnerMethod();
log.debug("someMethod");
}
public void someInnerMethod() {
log.debug("someInnerMethod");
}
}
package aop;
package com.gxlu.srm;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SomeServiceTest extends TestCase {
protected void setUp() throws Exception {
String[] paths = { "classpath:applicationContext-aop.xml" };
ctx = new ClassPathXmlApplicationContext(paths);
}
public void testAop() {
SomeService someService = (SomeService) ctx.getBean("someService");
someService.someMethod();
someService.someInnerMethod();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
<bean id="someServiceTarget" class="aop.SomeServiceImpl"/>
<bean id="someService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>aop.SomeService</value></property>
<property name="target"><ref local="someServiceTarget"/></property>
<property name="interceptorNames">
<list>
<value>someAdvisor</value>
</list>
</property>
</bean>
<bean id="someAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice"><ref local="debugInterceptor"/></property>
<property name="patterns">
<list>
<value>aop\.SomeService\.someMethod</value>
<value>aop\.SomeService\.someInnerMethod</value>
</list>
</property>
</bean>
</beans>
log4j.logger.org.springframework.aop=DEBUG
log4j.logger.aop=DEBUG
日志显示
[srm] 2006-12-24 23:06:16.953 < INFO> [main] org.springframework.aop.framework.DefaultAopProxyFactory.<clinit>(61) | CGLIB2 not available: proxyTargetClass feature disabled
[srm] 2006-12-24 23:06:16.984 <DEBUG> [main] org.springframework.aop.framework.ProxyFactoryBean.addInterface(216) | Added new aspect interface: aop.SomeService
[srm] 2006-12-24 23:06:17.015 <DEBUG> [main] org.springframework.aop.framework.ProxyFactoryBean.initializeAdvisorChain(420) | Configuring advisor or advice 'someAdvisor'
[srm] 2006-12-24 23:06:17.015 <DEBUG> [main] org.springframework.aop.framework.ProxyFactoryBean.addAdvisorOnChainCreation(526) | Adding advisor or TargetSource [org.springframework.aop.support.RegexpMethodPointcutAdvisor: advice [org.springframework.aop.interceptor.DebugInterceptor@126804e], pointcut patterns {aop\.SomeService\.someMethod, aop\.SomeService\.someInnerMethod}] with name [someAdvisor]
[srm] 2006-12-24 23:06:17.015 <DEBUG> [main] org.springframework.aop.framework.ProxyFactoryBean.addAdvisorOnChainCreation(535) | Adding advisor with name [someAdvisor]
[srm] 2006-12-24 23:06:17.015 <DEBUG> [main] org.springframework.aop.framework.ProxyFactoryBean.freshTargetSource(549) | Not refreshing target: bean name not specified in interceptorNames
[srm] 2006-12-24 23:06:17.015 <DEBUG> [main] org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(109) | Creating JDK dynamic proxy for [aop.SomeServiceImpl]
[srm] 2006-12-24 23:06:17.062 <DEBUG> [main] org.springframework.aop.interceptor.DebugInterceptor.invokeUnderTrace(57) | Entering invocation: method 'someMethod', arguments []; target is of class [aop.SomeServiceImpl]; count=1
[srm] 2006-12-24 23:06:17.062 <DEBUG> [main] aop.SomeServiceImpl.someInnerMethod(17) | someInnerMethod
[srm] 2006-12-24 23:06:17.078 <DEBUG> [main] aop.SomeServiceImpl.someMethod(13) | someMethod
[srm] 2006-12-24 23:06:17.078 <DEBUG> [main] org.springframework.aop.interceptor.DebugInterceptor.invokeUnderTrace(60) | Exiting invocation: method 'someMethod', arguments []; target is of class [aop.SomeServiceImpl]; count=1[srm] 2006-12-24 23:06:17.078 <DEBUG> [main] org.springframework.aop.interceptor.DebugInterceptor.invokeUnderTrace(57) | Entering invocation: method 'someInnerMethod', arguments []; target is of class [aop.SomeServiceImpl]; count=2
[srm] 2006-12-24 23:06:17.078 <DEBUG> [main] aop.SomeServiceImpl.someInnerMethod(17) | someInnerMethod
[srm] 2006-12-24 23:06:17.078 <DEBUG> [main] org.springframework.aop.interceptor.DebugInterceptor.invokeUnderTrace(60) | Exiting invocation: method 'someInnerMethod', arguments []; target is of class [aop.SomeServiceImpl]; count=2
单独进入'someInnerMethod'时 AOP起作用
但通过'someMethod'调用someInnerMethod时为什么没有出现Entering invocation: method 'someInnerMethod'的日志?
是否SpringAOP不支持嵌套?!