【问题标题】:How to programatically add beans in Java to the app-context.xml in Spring?如何以编程方式将 Java 中的 bean 添加到 Spring 中的 app-context.xml 中?
【发布时间】:2013-04-09 13:35:40
【问题描述】:

我正在关注本教程http://java-diaries.blogspot.com/2011/03/get-started-with-spring-jms-using.html,但我不明白为什么对于 @Service 和 @Autowired 注释,您需要在 app-context.xml 中显式创建 bean,但对于 @Component,它会为您创建一个.有人可以解释一下吗?另外,有没有办法使用注释添加目标和连接工厂bean?还是我需要类似Add Bean Programmatically to Spring Web App Context

的解决方案

【问题讨论】:

    标签: java spring activemq applicationcontext


    【解决方案1】:

    如果您使用context:component-scan 标记,则使用@Component 和@Service 注释的类将被检测到,并且不需要在您的应用程序上下文中声明。

    @Autowired 不同。当您声明一个字段 @Autowired 时,这意味着应用上下文中的 bean(无论是显式声明还是由组件扫描获取)都被注入。

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 2017-08-18
      • 2010-12-29
      • 1970-01-01
      • 2015-01-31
      • 2018-10-30
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多