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

Java Spring with Hibernate Integrating Hibernate with Spring Fetching Data with Hibernate in Spring

Sergei Makarov
Sergei Makarov
13,839 Points

Dependency Injection in Spring

Sorry, but I really do not understand Autowiring and Bean Annotations in Spring. Could anyone please explain them. In the course 'Hibernate with Spring' Chris has a teachers notes. Let's go through them, cause I've been confused a lot:

"Two Approaches for Autowiring Beans in Spring There are two approaches that are typically used for dependency injecting (DI) or autowiring objects in Spring. First Approach: Simple Implemented Interface

Using the @Autowired annotation in Spring defaults to finding exactly one class that implements the interface of the object marked as @Autowired. That is, any objects from the Spring library loaded at runtime, [..]."

So, let's take an Environment Interface for example. In DataConfig class we annotate it with Autowired. First of all Envirionment is Interface with a PropertyResolver superinterface, but there are 5 (!!!) classes, which implements those Interfaces. Chris says "finding exactly one class (!!!) that implements the interface". My question is: How does Spring know, which class we want to use ???

I have also some kind of misunderstanding with @Bean s, but I would like firstly understand @Autowired.

Appreciate your help!

2 Answers

Rodrigo Castro
Rodrigo Castro
15,652 Points

Hi Sergei,

In this video from the Spring Basics course, at about 5:40 Chris gives a very good explanation about the @Autowired annotation.


Hope it helps.

Hi, Sergei. I am confused too. It needs more practice to understand them. In my opinion, @bean should be created first. They should be some methods for returning instance of an object, in another words, they should always return some thing. After that, we have the @autowired. They are the corresponding instances which you created before with @bean. For instance, if you have a @bean method which return "session" class, then no matter where and how many times you declare a "session" instance in other classes, here it is always the same instance. And one of reasons for using @autowired, I think it is reusable. For instance One webapplication, simultaneously, could have thousands of customer, you shouldn't create thousands of instance for them. Then the better solution maybe is to reuse the same instance. Last, maybe I am totally wrong..... I am not professional.