hey guys welcome to my channel in this video and also the upcoming series of tutorials we will learn about Spring Framework and how to build a java application using Spring Framework we will discuss about what is spring white is used and what is the problem it is trying to solve and after that we will discuss an important and famous design pattern behind Spring Framework known as dependency injection so let's start but what is Spring Framework it is a Java framework which was developed to make the enterprise Java development easier so it was built taking many
best practices and and well-known design patterns into consideration you can also say that spring enables us to write better Java code you may ask me how it enables us to write better code good question I'm going to show you exactly how by taking a code example before diving into the code example I want to tell you that you need to have a basic understanding of Java programming language to benefit from this course so let's dive into the code here we have an email application the email application is a Java class and inside this class we
have our main method and inside this main method we are creating an object of class email client and after creating the object we are calling the send email method of the object by passing in the strings hey this is my first email message and hey this is my second email message let's go inside the email client class and here we have the send email method inside this method we are first performing a spell checking by using a basic spell checker class which is instantiated inside the constructor and using this object reference we are calling the
check spelling method of the basic spell checker class let's go inside this method here we are doing a check that the provided email message is not null and after that we have some print statements to simulate that this class is actually checking the spellings of our email message so let's look at what are the problems we are facing with this particular code so let's go back to our email flying class and here you can see that whenever we are creating an email client class we are also creating a basic spell checker class in other words
we can say that email client class is highly dependent on basic spellchecker class right in a software development terminology you can say that email client is tightly coupled to a basic spell checker class and having a tightly coupled design is very bad for your occasion it makes us hard to maintain the code hard to extend the code and also hard to unit test code we should always try to design our classes in a loosely coupled manner which would be the opposite of tight coupling to further explain what I mean imagine that we have created an
advanced spell checker class for our email client let's look at that class so here we have the same check spelling method inside our class and this method has a lot more functionality to it compared to our older basic spell checker instead of just doing a basic spell checking it's automatically detecting the language of the email message it is checking for the grammatical errors in the email message and finally each being for the spelling mistakes if you want to use this physical I have to change my email fine class and replace the basic spell checker implementation
with the advanced spell checker class so this is exactly what I mean by tight coupling if our spell checker implementation is changed we should also change our email client another drawback with this particular code is testability if you want to write a unit test for the send email method it's very hard to test whether this method is actually invoking the check spelling method or not because the basic spell checker class is not at all exposed out of the email client class we cannot replace this basic spell checker class with some dummy or mock spell checker
classes for unit testing so the first step to make this class loosely coupled is instead of using the basic spell checker class I created an interface called as spell checker and made the basic spell checker class implements that interface remember that the key for creating loosely coupled classes is to make use of Java interfaces and their polymorphic behavior so now instead of creating the basic spell checker class object I can pass a spellchecker object as the constructor parameter you and at the time of creating the email client I will pass in the new basic spell
checker as the argument so now we can say that email client is loosely coupled with spell checker interface so tomorrow if I want to change the basic spell checker with advanced spell checker I can just create a new advanced spell checker and pastured has a constructor argument that means we are providing the dependencies of the email client class instead of creating it directly inside the constructor of the email client class in other words we are injecting the dependencies into the email client class this process is called as dependency injection we are manually injecting the dependencies
to the email client class at the creation time so imagine that instead of just these three to four classes if our source code has hundreds of classes it will be very painful for us to instantiate the classes and pass those dependencies manually we need some kind of framework which creates this objects and their dependencies automatically that is where Spring Framework comes in the spring framework initially started out as just a dependency injection framework which creates our Java objects also called a spring beans and injects them at fronton now Spring Framework is not just a dependency
injection framework it has a lot of other projects if you go to a spring website the spring dot IO website and check their projects page you can see that we have lots of projects which are created around the course spring framework so I hope you have understood what is Spring Framework what is the reason behind using Spring Framework and in the next video I will show you how to set up a project using IntelliJ how to download spring and how to apply dependency injection principle we learned using Spring Framework if you like this video please
subscribe to my channel for more upcoming tutorials like this and I will see you later