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
There are three weaknesses of autowiring : Overriding: You can still specify dependencies using <constructor-arg> and <property> settings which will always override @Autowired . Primitive data types: Autowiring can't be used to inject primitive and string values. It works with reference only.
Since Spring is the most popular DI and IOC container for Java application, @Autowired is more common and @Inject is lesser-known, but from a portability point of view, it's better to use @Inject. Spring 3.0 supports JSR-330 annotation, so if you are using Spring 3.0 or higher release, prefer @Inject over @Autowired.
1) byName autowiring mode It internally uses setter injection. But, if you change the name of bean, it will not inject the dependency.
@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With