Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
hbdc
13,725 PointsWhat's the difference between using the DI with @Autowired and simply creating an object in Spring.
What's the difference between using the DI with @Autowired when creating a gifRepository object and simply writing out GifRepository repo = new GifRepository()?
Wouldn't that work and still be able to get the constant list for all of the objects?
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsCheck this answer here:
http://stackoverflow.com/questions/30908648/spring-autowired-vs-using-new-keyword-to-create-object
Quote from there:
The typical use of @Autowire is to automatically fill a property, when initializing a bean, with a singleton dependency. It does not matter if it's your code or a Third Party class. You need to consider if it is part of your program's logic or if it is really a dependency that should be initialized once and reused.
I guess the key word is singleton. If you don't know check about them:
https://en.wikipedia.org/wiki/Singleton_pattern
Saying very very roughly, we basically give the hard work of checking when to create or remove object, and how to make sure we are not creating objects over and over again with new.