8

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 applicationContext.xml.

My question is, how much of a performance impact does this have on my startup times? Would it be worth it to remove all of the @Autowired annotations and actually wire this application up manually via the applicationContext.xml?

From an architectural point of view, I like @Autowired. I don't want to add another layer of complexity by using the xml file - it adds no value as far as I am concerned. But if this sort of thing is adding 10 seconds to my container's load time, I may consider it. If the cost is 100 milliseconds, then I'll leave it as it is.

Thanks

egervari
  • 22,372
  • 32
  • 121
  • 175

2 Answers2

7

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)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks. Then I'm going to have to find a way to reduce the startup time in other locations. I do start/stop the server a lot as I change controller actions and test ajax. I just want to discover ways to reduce the startup time. I realize for production, this is a non-issue, but for development, this startup time is actually really serious. – egervari Nov 23 '11 at 21:56
  • I don't think that can be helped really. I still run a lot of application tests, and I do change annotations somewhat frequently that hotswap isn't an option. – egervari Nov 24 '11 at 07:22
  • 1
    This isn't true even for production. Specially with things like appengine. Startup time performance is extremely important. – Rafael Sanches Jul 13 '13 at 03:09
  • 1
    This also is a hit on testing. – allprog Aug 05 '13 at 14:48
5

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.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 4
    note that there's a difference between autowiring with annotations and component-scanning. You can switch off component scanning and still have @Autowired. – Bozho Nov 23 '11 at 22:03