Spring Dependency Injection - Overview
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 426,097 students
Learn more from the full courseSpring & Hibernate for Beginners (includes Spring Boot)
Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate
40:54:10 of on-demand video • Updated March 2021
- Develop a REAL-TIME project with Spring MVC, Spring REST, Spring Boot and Hibernate CRUD ... all from SCRATCH
- You will TYPE IN EVERY LINE of code with me in the videos. I EXPLAIN every line of code to help you learn!
- LEARN key Spring 5 features: Core, Annotations, Java Config, AOP, Spring MVC, Hibernate and Maven
- I am a RESPONSIVE INSTRUCTOR ... post your questions and I will RESPOND in 24 hours.
- POPULAR VIDEOS for: Spring Boot 2, Spring Security, Spring REST, Spring Data JPA, Spring Data REST and Thymeleaf
- Join an ACTIVE COMMUNITY of 185,000+ students that are already enrolled! Over 47,000+ Reviews - 5 STARS
- Students have LANDED NEW JOBS with the skills from this course. Spring and Hibernate developers are in HIGH-DEMAND!
- You can DOWNLOAD all videos, source code and PDFs. Perfect for offline LEARNING and REVIEW.
English Instructor: In this video, we're gonna cover spring dependency injection. So, what exactly is dependency injection? Here's a textbook definition, allows a program design to follow the dependency inversion principle. The client will delegate the XOR code, the injector, the responsibility of providing it's dependencies. Whoa (laughs) if that didn't confuse you, I don't know what will. So let's kind of break it down into normal layman's terms that a mere mortal can understand. In things that I can understand. So, say for example, I'm going to buy a car and this car is built at the factory on demand. So, there's nothing in the car lot. You have to actually talk to the factory and put in a request and they'll build a car for you; So, at the factory, you have all the different parts for the car. You have the car chassis, you have the engine, the tires, the seats, the electronics, the exhaust, and so on. And the mechanics or the assemblers there or the technicians, they'll actually aseemble the car for you and then deliver to you the final car. So, you don't have to actually build the car. The car's already built for you at the factory. So, they actually inject all of the dependencies for the car. So they inject the engine, they inject the tires, the seats and so on. So that's basically what you have here with dependency injection. So you simply outsource the construction and injection of your object to an external entity. In this case, that's the car factory. Alright, so that was a basic car example. How do this work in the Spring world? Well remember that Spring has am object factory, right? So then, when you retrieve an object like a coach object, this coach object may have some addition dependencies. So these dependencies are really just helper objects, other objects that it needs to perform it's operation. So instead of you have to manually build the coach object and all of it's dependencies, the Spring framework of the Spring factory will actually do this work for you. So just like you get a car that's ready to drive, here you'll get an object that's ready to use. Alright, so that's Spring container. So we've kind of covered the two major functions of the container. IN previous videos, we learned the idea of creating and managing objects with inversion of control and now here we'll focus on injecting object's dependencies with that dependency injection. And these are the two core features and we'll kind of have this all covered once we finish this section of videos. Alright so, we'll walk through this with an example again. So our coach already provides daily workouts. Now our coach will also provide daily fortunes, like your good luck fortune. This coach will actually make use of a helper called FortuneService and this is what they call a dependency because a coach depends on a fortune service in order to serve up the daily fortunes. Now, there's different types of injections you can use in Spring. Actually, there's a lot but we'll the two most common injection types that you'll use. It's called constructor injection and there's also setter injection. And we'll cover these in a set of videos here in this section. We'll also talk about auto-wiring beings in the annotations section that we'll cover a little later. Alright so, we'll start off with the constructor injection and here's the development process and you know me, I like to do things step by step. So the first thing we're gonna do is define the dependency interface in it's class. Then we'll go through and create a constructor in our class for injections and then the final step is configuring the dependency injection in the Spring configuration file. And again, we'll do all of this step by step and from scratch. Okay, so let's start with step one. Defining the dependency interface and class. So, in the top left, we're gonna have this fortune service. It's simply an interface that's gonna have a method called get fortune. And that returns a string and that's it. So it's simply the specification or the interface. In the bottom of the slide, we hace this happy fortune service. This person actually implements the fortune service and when I say implement, meaning, they provide an implementation of that method get fortune. So here, for the fortune, they'll simple hard code something to say, today is your lucky day and they return it as a string. So that's a very simple example of an implementation of the fortune service. You can think of more advanced implementations. Maybe an implementation that could read something from the file system or maybe from a data base or possibly a web service but we'll keep it simple for now and we'll talk about some of those other advance features a little later. Alrighty, so that's step one. So now, step two, create a constructor in your class for injections. So again, we're gonna make use of this constructor injection. So they're going to inject dependencies here by calling a constructor, so I have my class baseball coach. I need to create a constructor baseball coach that will accpet this dependency. So it has to accept a fortune service. So I have private field called fortune service and then I simply assign that field inside of a constructor here. Fortune service equals the fortune service. Okay great, now we configure the dependency injection in the Spring config file. So what I'll do at the top here in my Spring config application conteot.XML. What I'll do is I'll define this bean called my fortune service that's the ID, and tehn the class, the actual implementation class of that service and that's our happy fortune service that we just saw on the previous slide. So that's our dependency. Now, what we need to do Is actually inject that dependency into our class and how do we do it? So now I have my coach here, bean ID equals my coach, class, baseball coach. Now what I do is I tell Spring, hey, inject a dependency into this baseball coach. So I have constructor arg and I have ref equals my fortune service. That's the same ID name of the bean up top. So, ref equals my fortune service, that's the ID of the fortune service that we have up top, so they're gonna inject this. Now, what'll happen behind the scenes is that when Spring makes it to this line of code in your config file, they'll actually look at your baseball coach, they'll ball your baseball coach's constructor and they'll pass in a reference to the my fortune service. So Spring willa ctually construct your object and pass in the appropriate data into the constructor and that happens behind the scenes. Because remember, Spring has an object factory. Spring is responsible for creating objects and, also, it's responsible for injecting their dependencies. So we're kind of covering everything, right here in this configuration. Alright, so I know that was a lot of heavy stuff. What we're gonna is actually move into eclipse and then we'll actually write this code from scratch and we'll also run it so you can kind of see it real time as far as how all this works out. So hold tight and I'll see you in the next video.