Is Spring's @Autowired a huge performance issue?

前端 未结 2 1580
感动是毒
感动是毒 2021-02-09 14:31

I have a project that has... I dunno... 200-300 daos/services/controllers and I use @Autowired to wire everything together rather than specify everything in the

相关标签:
2条回答
  • 2021-02-09 15:15

    There is an interesting comment by @Masterhard in Spring @Autowired usage:

    We are switching from @Autowire back to XML configuration in our big project. The problem is very low bootstrap performance. Autowiring scanner loads all classes from autowiring search classpath, so, lots of classes are loaded eagerly during Spring initialization.

    Also see e.g. SPR-6870.

    However! Autowiring using annotations is so convenient that I would think twice before switching back to XML. Unless the startup time is really an issue in your project and you can prove that it's the CLASSPATH scanning that causes it, stay with annotations. Also remember that Java EE also moves towards annotations.

    P.S.: Parsing thousands of lines of XML also introduces some overhead.

    0 讨论(0)
  • 2021-02-09 15:32

    Practically the same. Component scanning is a bit more expensive (when you scan for @Service, @Component), but, as you said, it is startup-time - it happens only once. And on a moderate machine it starts pretty quickly even with annotations.

    Generally, I wouldn't abandon the approach just because it adds a bit of startup time. And I can assure you it is nothing significant (working on a bigger project than your right now)

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