繁体   English   中英

Spring 3中的@Autowired注释行为

[英]@Autowired annotations behavior in Spring 3

我想了解@Autowired注释的工作原理。

import com.test.WorkFlowDAO;

public class ServiceCentralBOImpl implements IServiceCentralBO
{
    /**
     * Logger for logging functionality.
     */
    private static final Log log = LogFactory.getLog(ServiceCentralBOImpl.class);

    @Autowired
    private WorkFlowDAO workFlowDAO;
    .
    .
    .
}

并且在我的Spring applicationContext.xml文件中声明了该bean:

<bean id="workflowDAO" class="com.test.WorkFlowDAO">
</bean>

通过仔细检查,您可以看到Java类中的两个ID和上下文XML文件是不同的。

workFlowDAO

workflowDAO

[两个ID中的字母'f'不同)

因为即使使用此配置,我的应用程序也可以正常运行; 我想知道@Autowired批注如何工作,以便在bean ID不完全匹配时不会抱怨。

如果使用简单的bean; Spring本来会抱怨bean名称不匹配。

我正在Websphere App Server 7.0上运行带有Spring 3.0.5的J2EE应用程序

让我知道是否需要更多信息。

@Autowired按类型匹配bean。 不考虑ID。

如果您在XML配置中声明了另一个相同类型的bean,Spring将抱怨无法确定正确的bean。

如果要将ID与@Autowired一起使用,则可以通过@Qualifier来使用,尽管在这种情况下建议使用@Resource

此处找到有关该主题的更多信息。

完全同意第一条评论。

如果希望按名称自动连接bean,则可以考虑使用@Qualifier(“ givenName”)。

查看所有详细信息:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM