我有以下定义:
<bean id="myInterceptor" class="info.fastpace.MyInterceptor"/>
<bean id="alikProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="myClass"/>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
</list>
</property>
</bean>
我定义了 2 个类:MyInterceptor
和 MyClass
。
奇怪的是,Spring 知道在调用类 MyClass
(大写 M)之前调用拦截器,即使该类未在 commonContext.xml 文件中配置。唯一的提示是代理 bean 定义中的 myClass
(小写 m)。
删除 alikProxy bean 定义时,不会调用拦截器。
Spring 如何知道使用 未定义 myClass
引用来调用 MyClass
的拦截器?
最佳答案
看起来您定义了一个类 MyClass
的 bean ,但没有显式地给它任何名称,所以Spring只是给了它一个默认名称 myClass
,基于类名MyClass
.
更新
我想在你的 Spring 上下文 xml 中的某个地方你有 <context:component-scan>
元素。
这是书 Spring In Action, 3rd Edition 的片段:
By default,
<context:component-scan>
looks for classes that are annotated with one of a handful of special stereotype annotations:
@Component
—A general-purpose stereotype annotation indicating that the class is a Spring component...skipped...
For example, suppose that our application context only has the
eddie
andguitar
beans in it. We can eliminate the explicit<bean>
declarations from the XML configuration by using<context:component-scan>
and annotating theInstrumentalist
andGuitar
classes with@Component
....skipped...
When Spring scans the
com.springinaction.springidol
package, it’ll find thatGuitar
is annotated with@Component
and will automatically register it in Spring. By default, the bean’s ID will be generated by camel-casing the class name. In the case ofGuitar
that means that the bean ID will beguitar
.
关于java - 为什么 Spring ProxyFactoryBean 隐式定义有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29628074/