Why do I need a setter for autowired / injected field?

后端 未结 3 1866
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 00:16

I have a bean:

    
        

        
相关标签:
3条回答
  • 2021-02-06 01:03

    My understanding is that XML configuration overrides annotation config.The fact that autowire="byType" specified overrides the auto injection, which looks for a presence of setter method for injecting the dependency.

    0 讨论(0)
  • 2021-02-06 01:08

    You need to use a setter because annotations are not detected unless spring is told so through either <context:component-scan /> or <context:annotation-config />. Setter is detected because you specified autowire="byType".

    You may find this question and answer helpful as well: When to use autowiring in Spring

    0 讨论(0)
  • 2021-02-06 01:17

    First of all, the use of <context:component-scan /> or <context:annotation-config /> enables Spring to scan your code for eligible beans to meet dependencies, which will greatly improve it's ability to wire them up correctly, so I suggest adding them to your context file.

    Second, you should be aware that @Inject is a standard (meaning JSR-330 specification) annotation. It is okay to mix and match Spring annotations with standard ones, but behavior may vary when doing so. @Named is commonly paired with @Inject to match components with dependencies (both JSR-330). See this reference for details, and refer to Table 4.6 for usage comments.

    But to directly answer your question, "why do I need a setter when not using component-scan", is because you are not using component-scan. You are asking Spring to inject a dependency "byType", but not allowing Spring to scan your code for components which are of that type. The reason the setter works is that the type of the setter argument being injected can be discovered by Spring in the compiled bytecode (i.e. meta-data), and so it successfully resolves your request.

    0 讨论(0)
提交回复
热议问题