【问题标题】:Why does spring invokes so many methods before a method of a spring bean has been invoked?为什么spring在调用spring bean的方法之前调用了这么多方法?
【发布时间】:2014-11-13 09:26:55
【问题描述】:

假设我在 spring 配置中指定了一个类,如下所示:

<bean id="playerAccountDao" class="com.prcj.db.dao.PlayerAccountDaoImpl" scope="prototype">
    <property name="playerDao" ref="playerDao" />
</bean>

<bean id="commonPlayerDao" class="com.prcjcommons.db.dao.PlayerDaoImpl" scope="prototype">
    <property name="sessionFactory" ref="siteSessionFactory" />
</bean>

在上面的配置中,我将该 bean 注入到 playerAccountDao 实例中,如果我尝试从 playerAccountDao 调用 playerDao bean 的方法,则会调用一些方法:

NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
Method.invoke(Object, Object...) line: 606  
AopUtils.invokeJoinpointUsingReflection(Object, Method, Object[]) line: 310 
ReflectiveMethodInvocation.invokeJoinpoint() line: 182  
ReflectiveMethodInvocation.proceed() line: 149  
TransactionInterceptor.invoke(MethodInvocation) line: 106   
ReflectiveMethodInvocation.proceed() line: 171  
JdkDynamicAopProxy.invoke(Object, Method, Object[]) line: 204   
$Proxy151.selectRecordList(Long, Integer, PlayerStatus, UserCategory, Date, Date, int, int) line: not available 

spring 为什么要这样做?为什么不直接调用DAO 方法呢?

【问题讨论】:

  • Spring 提供了 AOP 特性,这就是为什么你这样的堆栈跟踪

标签: java spring


【解决方案1】:

让我反过来说:为什么要编写调用其他方法的方法,而不是直接更改变量?

这只是另一种间接形式。 IoC 可能非常复杂,而且 Spring 有很多特性可以为用户带来好东西,所以它有很多情况需要支持。拥有这些不同的调用策略和拦截器意味着它们可以避免重复,共享共同的方面,同时允许不同的行为。

如果他们只是调用 DAO 方法,那么他们的大部分功能集都无法支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2019-04-26
    • 2016-06-26
    • 1970-01-01
    相关资源
    最近更新 更多