【问题标题】:is using @Component compulsory for using @Autowired using <context:component-scan base-package="org.controller"/>使用 @Component 强制使用 @Autowired using <context:component-scan base-package="org.controller"/>
【发布时间】:2019-07-30 13:41:29
【问题描述】:

我想同时使用@Autowired 注释 我想知道@Component 是否应该与它一起被强制使用,或者它是否可以在没有@Component 的情况下工作

【问题讨论】:

  • @Component 告诉 spring 哪个 Class(Bean) 注入,@Autowired 告诉注入哪里,所以它们是相互关联的,不,如果你不使用 @987654325,你不能使用 @Autowired @ 或其子公司 (@Controller ,@Repository,@Service)
  • 但我正在使用 以及我将在 xml 本身中配置 bean,例如 这会告诉spring哪个是bean,然后Autowired会告诉在哪里注入那个bean,现在组件是强制性的吗?
  • 不,那么它不是,xml 将进行 bean 部分的注入 :)

标签: java spring


【解决方案1】:

为了使用@Autowired 注入任何 Spring bean,您需要使用以下解决方案之一声明/创建它们(如果尚未由 Spring 本身创建):

一旦你声明了一些bean,你就可以使用@Autowired注解注入它们。另见http://olivergierke.de/2013/11/why-field-injection-is-evil/

【讨论】:

    猜你喜欢
    • 2017-02-04
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    相关资源
    最近更新 更多