spring aop 2

本文探讨了Spring AOP中部分方法调用未记录预期日志的问题。具体表现为直接调用'someInnerMethod'时能正常记录日志,而通过'someMethod'间接调用时则无法记录。文章提供了完整的代码示例及配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请先看代码

Java代码 复制代码

 

package aop;

public interface SomeService {
	void someMethod();
	void someInnerMethod();
}

 

Java代码 复制代码
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");
	}

}

 

Java代码 复制代码
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();
	}
}



Java代码 复制代码
<?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>

 

Java代码 复制代码
log4j.logger.org.springframework.aop=DEBUG
log4j.logger.aop=DEBUG



日志显示

Java代码 复制代码
[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不支持嵌套?!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值