gpt4 book ai didi

java - 为什么 Spring ProxyFactoryBean 隐式定义有效?

转载 作者:行者123 更新时间:2023-12-01 11:39:48 28 4
gpt4 key购买 nike

我有以下定义:

<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 个类:MyInterceptorMyClass

奇怪的是,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 and guitar beans in it. We can eliminate the explicit <bean> declarations from the XML configuration by using <context:component-scan> and annotating the Instrumentalist and Guitar classes with @Component.

...skipped...

When Spring scans the com.springinaction.springidol package, it’ll find that Guitar 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 of Guitar that means that the bean ID will be guitar.

关于java - 为什么 Spring ProxyFactoryBean 隐式定义有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29628074/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com