0% found this document useful (0 votes)
5 views2 pages

Dependency Injection Techniques in Spring

This lesson from Learn Spring covers simple bean wiring and dependency injection methods in Spring. It discusses three primary ways to inject dependencies: constructor-based, setter-based, and field-based injection, detailing how the Spring container manages these processes. Practical examples are provided to illustrate each method, emphasizing the simplicity and cleanliness of constructor injection as the preferred approach.

Uploaded by

Sergio Cabrera
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Dependency Injection Techniques in Spring

This lesson from Learn Spring covers simple bean wiring and dependency injection methods in Spring. It discusses three primary ways to inject dependencies: constructor-based, setter-based, and field-based injection, detailing how the Spring container manages these processes. Practical examples are provided to illustrate each method, emphasizing the simplicity and cleanliness of constructor injection as the preferred approach.

Uploaded by

Sergio Cabrera
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Eugen: Welcome to this lesson out of Learn Spring, where we're going to be discussing how to

do simple bean wiring and injection. All right, so now that we have a good
understanding of how to create a bean and how to contribute that being back into the
application context, let's now have a look at defining relations between these beans. We
talked about this in a general way when we first discussed the dependency injection, but
now let's make this more [00:00:30] concrete and let's see how we can set up these
dependency between two beans.

To be clear, this is the responsibility of the spring container. The container will do this
injection process for us. Now there are primarily three ways of defining and injecting
independency. One of the ways we can inject the bean into another bean is simply via
the constructor. Another way is of course via is standard setter, [00:01:00] and finally,
we can also do injection via a field.

All right, let's jump right into the practical aspects of these lesson, and let's try these out
in practice. Okay, so let's get started here with constructor based injection. We have our
starting point of the lesson imported here and we can now inject the bean into another
bean via a constructor argument. Let's do that in our service class. This is the style
[00:01:30] of injection that we are already using so there's technically nothing to be
done here. However, notice how we are injecting the project repository through the
constructor of the service implementation here. What will happen is during the
bootstrapping process, the container will access the project repository bean and it will
inject it here in our service class.

Now the nice thing about constructor injection and really the reason that this is our
default is [00:02:00] that it is simple and clean, and for the most part doesn't really use
any spring specific annotations or explicit logic. For example, if you're looking at the
imports here, you'll notice that the only spring aspect here, the only spring dependency
that we have is the service annotation. We don't have anything else. All right, so that is
dependency injection via the constructor.

Now let's move on and let's discuss [00:02:30] setter based dependency injection. With
this style of injection, we're going to be injecting our dependencies using the setter
methods of the target bean. In order to do that, let's create a new service class and let's
use this technique. Okay, so let's delete the constructor since we're no longer [00:03:00]
going to need it, let's create a setter for the project repository. We don't really need a
getter, and now all we have to do in order to mark the project repository bean to be
injected here is to just auto wire the setter. That is basically it.

Now let's make sure this works. Let's target up the project. [00:03:30] Have a break
point right here in the center, and of course, make sure that this is actually called during
startup. All right, so this is setter injection working.

Let's move on now and let's talk about field based dependency injection. This is a really
common way to do dependency injection, but it has its drawbacks. We'll talk about
those in a second. [00:04:00] First of all, let's do it and let's understand it. As before,
we're going to create another class here. Let's delete the setter, and as you may already
Page 1 of 2
imagine, field injection means auto wiring the field directly. [00:04:30] That is all we
need to do. Similarly to all of the other types of injection, the spring container will now
know that this field needs to be injected as a bean, and will of course perform that
during startup during the bootstrapping of the container.

Page 2 of 2

You might also like