Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Spring's @Autowired a huge performance issue?

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

like image 495
egervari Avatar asked Nov 23 '11 21:11

egervari


People also ask

What are the disadvantages of Autowired?

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.

Should I use inject or Autowired?

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.

Which Autowiring is better in Spring?

1) byName autowiring mode It internally uses setter injection. But, if you change the name of bean, it will not inject the dependency.

What is the difference between @autowired and @bean?

@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).


1 Answers

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)

like image 134
Bozho Avatar answered Sep 28 '22 02:09

Bozho