Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc.
Here we will discuss about some important concepts about modules provided by Spring framework such as Spring IOC,AOP,MVC. Spring also comes with a combination as Spring-Rest. We shall discuss this also later in this topic.
Lets start with Spring IOC.
In earlier days when Spring was not so popular, we came across a concept of tight coupling where one class when instantiated directly inside another class led to tight coupling which usually failed the Object oriented design. Consider the scenario as below.
Here as you can see, the class Student is directly instantiated in class College, which results in tight coupling. Spring IOC comes to the rescue. Please check below diagram for Spring IOC and we will discuss what it is.
Spring IOC also called as Dependency Injection is a way by which objects get their dependencies at run time through Dependency injection. Objects don’t have to deal with dependencies explicitly by themselves. The dependencies are specified in the xml and objects don’t need to know their dependencies at compile time. Spring container provides the dependencies at run time to the objects. Now we shall see below how does the spring IOC works. Consider below xml.
When the spring container starts, the beans inside the xml file are instantiated and the dependency of bean student to college is given at run time through different types of injections. Usually we use setter injection or constructor injection.
Consider below cases
Setter injection – Setter injection is a optional dependency.
Constructor injection : This if provided, is a mandatory dependency.
In the above xml, you saw we injected student bean inside college using tag for setter injection , for constructor injection you can use . This is fine.
But now-a-days use of xml is becoming very less due to its heavy loading. We have a concept called Autowiring in Spring, where the injection is done inside the java class using @Autowired annotation instead of using or in xml.
Consider below case:
The resultant xml will be:
As of now this is enough to get started with concepts of spring IOC. Feel free to comment in comments section.