[MUSIC] So far we've seen that we can use Spring to automatically configure and launch our application. But one of the questions we haven't answer is, how does Spring know how to construct our application? What if our video service has some dependency on another object that we would like to be able to configure when we launch the application, so that we could provide for example an interface to the different ways that we store videos in memory. Or potentially even store them on disk, and then plug in different classes that implement that interface into the video service depending on our needs. Spring supports a facility called Dependency Injection, that we've talked about. That can automatically provide the bindings between the actual implementation of an interface and the usage of that interface in our various controllers. We're other other classes, so let's take a look in this example of how we can use this to our advantage. So in this version, if you open up the video service again, we'll see that we've added a new concept. And that is rather than using a direct list of videos that we're going to store in memory, instead we've created an abstraction called a video repository. And if you'll look down here a little lower, we'll see that this video repository has similar methods to what we had before. It has a method to add a video, and it had, it has a method to get the list of videos that have currently been added. So if we go, and actually look at this video repository. We'll see that it's just a simple Java interface that provides this functionality that we would expect of some storage system for storing videos may be it in memory or somewhere else. So once we've defined this interface, we can plug different implementations of the interface into the video service and not have to change how video service operates directly. Instead, if we wanted to provide an implementation that does something different, for example, one version might allow duplicate videos to be stored. Whereas, another version may not allow duplicate videos to be stored. It may only store one copy. So if we want to be able to provide those different implementations of that interface, we can use the interface here in the video service, and then add and get the videos directly using that interface. Now if you see this, this brings up a question, which is, we aren't directly instantiating our video service object. Instead, we are asking sprain to go and launch a web container, and to automatically discover our controllers and put them into the web container. So if we aren't directly instantiating some implementation of video service, and assigning it to this member variable. How does Spring know what should be assigned to this variable and how to fulfill this dependency? Well, to answer that, we can look at the application class, which is where we do the initial launch of our, our application and tell Spring to go and configure us. And what we'll see here is that we've listed at enable auto configuration and at component scam which as we've talked about before tells Spring to go and look in a particular package for all of our controllers when other components. And then the at enable auto configuration says, go, and automatically configure them. And we talked a little bit about this before. So what's going to happen is, when Spring goes and scans our controller, in this case the video service, oOne of the things it's going to see is that this member variable has an at autowired annotation. And what this at autowired annotation tells Spring is, this is a dependency that I require that I want you to automatically provide for me when you construct me. So Spring is going to have to figure out how to resolve the dependency and find some object that implements video service and automatically set the value of this member variable to a reference to that object. So if you go and then look in application, what we see is that we have a method called video repository. That's annotated with @being. And what this method does, is it tells Spring, here is the implementation that I want you to use for the video repository. And so what Spring is going to do is when it goes and looks at the video service, it's going to see that it needs to have a video repository implementation to @autowire into this member variable. So it's then going to go look at the application class which is the configuration of the application and say are there any @ being annotative methods that return an instance of a video repository. And in this case, it's going to say this method video repository will return an instance of a video repository. So it's going to call this method. It's going to get back the instance or the specific implementation of the video repository that we create here. And then, it's automatically going to take that value and assign it to the video's member variable and video service, and anywhere else that we refer to video repository and annotate it with auto-wired. Spring will automatically go and find those places where we are expecting that video repository to be at autowired and it will automatically assign those member variables the repository implementation that we returned here. So the way to think about this is that Spring is providing us a way to where we can have an interface that is used by multiple class. So, let's say that these are the users, or the class users that depend upon our interface. So we have a series of objects that depend upon an interface or some object being provided to them. And then our application where our configuration object 'cause it doesn't have to be called application provides the actual implementation that gets used at run time. So, Spring goes and finds all of the users because the users have @Autowired member variables that refer to the type. So, if a particular member variable is @Autowired annotated. Then Spring has to go and look in the configuration objects that we provide to it, in this case, application. It's going to find a method that returns an implementation of that particular interface. And then, it will automatically assign whatever value is returned from that method to all of our member variables are places where we refer to it in other classes. So our class users have the autowire on the place where they need a particular dependency like a video repository. And then, our application provides the implementation of all of those used interfaces that are at autowire. And Spring automatically figures out how to solve the complex dependency graph that we have to deal with in order to instantiate the various objects. So it will automatically figure out what order things need to be instantiated in, do that instantiation. And then assign all of the actual implementations of an autowired interface to all of the places it's referred to. And if for some reason we have something marked as @Autowired that doesn't show up in your application, then when when you try to startup, Spring is automatically going to throw an exception. So, in this example here where we are creating the actual implementation of the video repository. If we want to simply change the implementation that spring is going to use, all have to do is change this to a different implementation of video repository. And when we start up our application, Spring is automatically going to take this new implementation that we are creating here. And bind it to all of the places where we refer to a video repository. So, in order to have different configurations of your application, you can create different classes that implement at configuration and provide different values for the different dependencies that are supposed to be at auto wired into your application. [BLANK_AUDIO]