-1

I was informed by a colleague not to use annotations(@Autowired @Resrouces) to inject services, but I don't know the reason for this,What problems arise if you use annotation injection? can anyone help me?

I'd like to know the reason.can anyone help me?

  • https://stackoverflow.com/questions/4093504/resource-vs-autowired – Jens Aug 04 '23 at 09:05
  • If you use constructor injection then you can find curcular dependency at compile time while injecting with annotation you will come to know when application runs. Also, While writing JUnit test case its easier with contructor injection not require mocking injection. – Dhaval Gajjar Aug 04 '23 at 09:07
  • colleague tell me ,annotation injection is not recommended because of previous circular dependency problems, so now all use constructor injection – 杨泽宇 Aug 04 '23 at 09:20
  • I think the message was more as to not use field injection with `@Autowired` but use constructor injection. Depending on how much constructors you have you might still need to add `@Autowired` to instruct spring which constructor to use for autowiring, when there is just 1 it will be automaticlaly picked up. So I doubt it is annotation based but rather the field injection that should be avoided. I think your collegeague hasn't understood those things. – M. Deinum Aug 04 '23 at 10:14

0 Answers0