Inversion of Control: Spring Tutorial

Chad Darby
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 392,118 students

Learn more from the full course

Spring & 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:47:36 of on-demand video • Updated October 2020

  • 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 175,000+ students that are already enrolled! Over 44,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 going to cover Spring Inversion of Control. Now in the previous videos, we were working on our application. However, we didn't get it to a hundred percent because we didn't have the support for the configuration. And so we need to make use of an object factory to kind of help us out with this solution. And as I mentioned earlier, this is exactly what Spring comes into play. So Spring provides an object factory so we can have our application talk to Spring, say hey, give me an object. Based on a configuration file or annotation, Spring will give you the appropriate implementation. So then your app is configurable and will have full support for that based on our application requirements. So this looks pretty good. All right, so now we're into some real Spring stuff. So the Spring container, the primary functions of the Spring container is, one, create and manage objects, that's your inversion of control; and then two, inject object's dependencies, and that's what they call dependency injection. Now we'll actually cover all of this in the course. I'll start off with inversion of control, and then the following videos we'll actually get into dependency injection. There's three ways of configuring the Spring container. One is by using an XML configuration file, the other is using Java annotations. And then finally, you can make use of Java source code. And we'll actually cover all of these approaches or all of these techniques in this course. So let's talk about the XML configuration file. It's the original configuration file that was used when Spring was first released, so it's legacy, but we'll still cover it because most legacy apps still use this. So if you're joining a Spring team or you're joining a company that has existing Spring applications, it's a very high likelihood that their applications still use XML config files. So we'll start off with that. Also, Java annotation is kind of like the latest fad, the latest fashion, making annotations. And also, you can actually get rid of all config files and you can configure the container by using Java source code. But again, we're going to cover all of these techniques so you'll understand how each one of them works for your application. All right, so here's the Spring development process. And as you know, I luv to do lists, I luv step-by-step approaches. So the first step is configuring your Spring beans. The second step is creating a Spring container. And then the third step is retrieving the beans from the container. And we'll actually cover all of this step by step so you'll see how everything works, and we'll do all of this from scratch. All right, so let's start with step one, configuring your Spring beans. So in this example, I'm going to make use of the XML configuration file. So here in the XML config file called applicationContext.xml, I have a bean. I give the actual bean id, myCoach, and then I give the class. And that's the fully qualified name of the Java class. So the id, that's simply the id that your Java application will use when they want to retrieve a beam from the Spring container. And again, class is the actual class or implementation that you'll have for your application. Okay, so let's move to step two. So step two is creating a Spring container. So in the Spring world, a Spring container is generally known as an ApplicationContext. So that's the term that you'll see a lot in Spring documentation and in Spring apps. So they have specialized implementations of it, one for reading XML from the class path, an AnnotationConfig, a WebApplicationContext and so on. And we'll take a look at all of these in this course. All righty, so creating the Spring container. So in this example, I'm going to read an XML file that's on my class path. So I make use of this ClassPathXmlApplicationContext. And I construct this object and I pass in the actual name of the configuration file that I'm using for Spring. So from step one, we had a file called applicationContext.xml. You can use any name that you want as long as you're consistent between step one and step two as far as reading that configuration file. All right, so we have this container create it. Now the next step is retrieving the beans from the container. So your application is simply going to talk to the Spring container and say hey, give me a coach object. And based on information in the configuration file, it'll give you an implementation of that given interface. So here's step three as far as code. So the previous line we already created our context, now we retrieve the bean from the container. So here I say context.getBean, and here I say myCoach comma Coach.class. So myCoach relates to the actual bean id to find in the configuration file. You have to make sure those match up with the exact same case. And then Coach.class, that's the name of the actual interface that BaseballCoach implements. So here, Coach.class is the interface, BaseballCoach is the actual implementation. And then you assign it. And that's basically what you have to do for retrieving a bean from a Spring container. All righty, so that's the coding here. In the next video, we're actually going to move into Eclipse. We'll get our hands dirty, and we'll actually write this code and we'll run it, and then we'll actually see it in action. So woohoo!