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

标签 java spring

我有以下定义:

<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/

相关文章:

java - Cassandra 卡在 Initializing IndexInfo 上

java - 该程序是否引入了并行执行?

java - Spring JMS 监听器中的事务管理

java - 不同 RDBMS 上的 NamedParameterJDBCTemplate 和函数语法

java - Spring - 仅在保存/POST 时出现 'field list' 中的未知列(但读取/GET 有效...)

java - Facebook 的 Spring Social - 获取用户位置

java - Minecraft ScheduleSyncRepeatingTask Bukkit 到另一个类

java - "generalizes the tag object' s 存储”是什么意思?

java - Java的异常处理机制是责任链设计模式的一个例子吗?

Spring MVC 和 @Validate : Perform validate only on specific condition or if user changes the property