Spring Overview
Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.
Spring is not just a core framework; however, it might have started that way. There are a lot of projects underneath it to do different kinds of tasks!
- RESTful services and Web Applications using Spring MVC
- Data Access using Hibernate, Spring Data, Spring JDBC etc.
- Authentication & Security
- Test cases
- Spring Boot which comes with pre-built production-ready features
They are all part of the Spring family of projects known as ‘Spring Eco System’.
Contents
Prerequisites
Core Java: Core Java is the part of Java programming language that is used for creating or developing a general-purpose application. It is not possible to develop any advanced java applications without the knowledge of core java. Here’s a Core Java Application for demo purpose.
Servlet & JSP: Most Java web frameworks, including Spring MVC, use servlets behind the scenes. Servlets are based upon a low-level API for handling requests and responses. Spring uses it for the web application over HTTP. It is nice to have the knowledge of Servlet & JSP before proceeding further. Here’s a web application using Servlet & JSP for demo purpose.
Core Concepts
The underlying mechanism that Spring uses is the Spring Container.
Spring Container: The Spring IoC Container is at the core of the Spring Framework used to manage automatic dependency injection throughout the application. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction.
The container just uses DI to manage the components that make up an application. These objects are called Spring Beans.
The container gets its instructions on what objects to instantiate, configure, and assemble by reading the configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code. The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application.
Spring provides the following two distinct types of containers.
- Spring BeanFactory Container: This is the simplest container providing the basic support for DI and is defined by the org.springframework.beans.factory.BeanFactory interface.
- Spring ApplicationContext Container: Application context is an implementation class for IoC container. Spring manages our application services and object instances using Application Context. This container is defined by the org.springframework.context.ApplicationContext interface.
The ApplicationContext container includes all functionality of the BeanFactorycontainer, so it is generally recommended over BeanFactory.
//without using ApplicationContext
public void AccountService {}
public void Account {
AccountService accountService = new AccountService();
}
//using ApplicationContext
public void AccountService {}
public void Account {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //will discuss about it later
AccountService accountService = (AccountService)context.getBean("some_account");
}
<bean id="some_account" class="com.packagename.AccountService"> //beans.xml