What is the Spring framework really all about

this is a 10-minute introduction to the spring framework spring is a very popular application framework in Java and it's actually been around for a while Dom even transforming over time in this video you will learn what Spring Framework is actually all about why this is framework exists what does it do and more importantly why and when should you have to use it let's find out [Music] I've covered in another video how there are a lot of things that application developers in Java tackle in common even if you are working on different applications this is the reason why we have a lot of frameworks in the first place to provide structure and common patterns to make the process of building these applications easier to learn more check out this video that explains what frameworks really are and kind of the difference between frameworks and libraries so a spring framework is like you can guess it's a framework so it provides the patterns and the structure for your java applications and in the process it handles these common things that most developers need to do when they build a java application so what are these common problems that the Spring Framework addresses there are three major things and we'll take a look at all these three in this video first one to understand the first concept let's examine the way a typical java application behaves in the wild let's say you've created a java application with a bunch of classes when you run the application the application starts up objects are created and there's probably gonna be all these object instances in memory that correspond to these classes additionally these objects are very likely going to be related to each other in some days so with every job application after all the application initialization is done there is kind of like a network of objects connected to one another using object references this is a network of objects that reflects your business logic what happens after you end the application and let's say you start it again the same initialization logic is probably going to run and the state of objects in memory is going to be exactly the same as before but after the initialization code is done and the application starts executing whatever logic it needs to there's a chance that this network of objects changes in morphs over time as objects are added and removed but the point is that the initialization of objects in the object graph should pretty much be in a specific way as designed at the very first moments of application initialization now how do these objects get created and get references to each other well one way they can do that is by creating a new instance of the classes in their methods so let's say you have an instance of a class for that needs an instance of a class bar the class foo can have code that says new bar and now foo has an instance of bar but there's a problem with this approach if you have every object create its own instances and all the instances that it needs you pretty much have like a tree of objects it's hard to kind of build an interconnected graph of objects using this mechanism you might need some way of instances to not just blindly create new instances but instead to share multiple existing instances this sharing of existing instances is especially important for business services in most applications a lot of business services in a typical job application you have various different kinds of objects and classes there are some objects that are data objects for example account information right so let's say a user has two accounts the application creates to our current objects to hold that data they are data objects there are other kinds of classes and objects that are not meant to hold data but to perform some functionality so let's say you have an account service class that has methods to fetch users accounts the account service class does not hold any data but it has functionality it has methods to fetch and process accounts so now how many instances of the account service class does an application need to create at runtime well really just needs one you don't need multiple instances of the account service class it's not meant to contain any data and its member variables so even if you were to create like ten instances of the service class they will all be identical instances so for any such business service class that has just functionality to offer you ideally want to create just one instance of these classes for the whole application and every class that needs such services should ideally share the same instance now how do you do this creating new instances is easy you can instantiate objects anytime in your code but now how do you share instances this is the tricky part there is a design pattern actually called the singleton pattern and you can implement this to allow only single instances for every one of those classes but there's whole lot of coding and management that you will have to do to manage the creation and lifecycle of those instances so this is the first problem there Spring Framework tries to help when you add Spring Framework in your application it access sort of like a container for your object instances it wraps your application in a wrapper that's called the application context the spring application context and spring manages that rather than you managing the initialization of business services you have spring Manish them you basically tell spring what your business service classes are for example and you let spring initialize them and manage them you can tell it to create one instance for the whole application or multiple instances or whatever else you need spring looks at this and says okay I got it I'll create these instances and manage them for you now not only a spring creating and managing these singles and instances it also connects these objects together just the way you want that in your object graph picture the way it works is every class declares the dependencies that it needs let's say a class says ok one an instance of class a an instance of Class C and one of class F so spring is going to look at this declaration it'll inject those dependencies to make sure that every object has references to all the other instances they require this happens through a process called dependency injection I have a quick ten minute video explainer about what dependency injection is actually all about so check it out so this is the first problem that the Spring Framework solves managing object life cycles in dependencies for the components and services in your code through the application context and through dependency injection now on to the second problem when you look at Java applications in general about 98.7% off applications need to connect to a database don't ask me where I got that number I just made that up but honestly I wouldn't be surprised of an overwhelming number of applications need to connect to some database or the other for most applications is what really brings an application to life what gives meaning to the application however the traditional standard way of connecting to a database in Java is using something called JDBC to be able to use JDBC not many people who actually use JDBC like JDBC it's very very painful I've actually gotten requests to make a course on JDBC for Java brains and even though I kind of know enough to make a tutorial whenever I start thinking about making it I'm just like oh no this is it's not it's not very pleasant so here's the puzzling thing data connectivity is one of the most common things Java developers do but the way to work with databases in Java through JDBC is so unappealing so here's another common problem and here's where spring comes up for the solution the spring framework comes with various different data access api's and mechanisms for connectivity querying and transaction management all the essential stuff when it comes to working with databases in Java it allows you to continue to work with JDBC but kind of hides away all the nasty stuff from you and makes the experience much easier all right on to the third thing many enterprise applications are web applications they need to either serve up dynamic web pages HTML pages as front-end web applications or they need to expose REST API so that could be consumed by other api's or by single page applications or any other rich client JavaScript applications to facilitate this Spring Framework comes with the web framework called spring MVC spring MVC lets you easily create web applications and REST API using the same spring application model and dependency injection concepts so we've looked at three main things that spring framework brings to your applications first is managing your services and object instances using an application context managing dependencies using the dependency injection so that's the first thing second providing data access to simplify connecting to and working with databases and third the ability to easily build web vacations using spring MVC there's more though there are other things that spring framework provides I'm not covering it here I have kind of covered the basics and the most important parts here you should check out the documentation for more there is however another aspect of spring that you should know spring is not just the core framework but it started out that way but there are a whole lot of projects that do a whole lot more and they're all part of the spring family of projects in that sense spring is not just the core framework it's an ecosystem for example there is a project that lets you handle application security that's a project called spring security there's a project that lets you build cloud native applications called spring cloud there is a lot so the best way to find out more is to go to spring that IO slash projects and there you can learn about everything that the spring ecosystem can give you it's a list of projects in the spring ecosystem and you can learn more over there this was a 10 minute introduction to the spring framework and what it does ready to start learning more check out the links here and thanks for watching