JAVA Tutorials

What is the Spring Framework in Java?

Spring is a lightweight, open-source, Java application development framework. It is used to create an easily testable and reusable code. The size of the basic version of Spring Framework is 2MB; hence it is said to be lightweight.

Basic Concepts of Spring:

  • Inversion Of Control: In this principle, objects do not depend on the other objects but have knowledge about their abstractions for further interaction.
  • Dependency Injection: In this concept, one independent object uses other objects with the help of interfaces. Dependency is transferred at the time of creation. DI can either be implemented with the help of setters or passing parameters in the constructor.
  • Aspect-Oriented Programming: This helps to distinguish cross-through functionality in an application.

The architecture of Spring Framework:

Spring Framework contains around 20 modules, which are required for the application development.

https://www.jcombat.com/wp-content/uploads/2015/11/springarchitecture.png

Core Container:

  • Core: It contains all the fundamental features of the framework, such as Dependency Injection and IoC.
  • Beans: It provides the Bean Factory. 
  • Context: It acts as a medium to access any objects that are created.
  • Expression Language: It provides a language for querying and manipulating an object graph.

Data Access / Integration:

  • JDBC: It provides a JDBC-abstraction layer that removes the need for JDBC coding.
  • ORM: It provides an integration layer for object-relational mapping.
  • OXM: It provides an abstraction layer to support Object-XML mapping.
  • JMS: Java Messaging Service provides features for producing and consuming messages.
  • Transactions: It supports programmatic and declarative transaction management.

Web (MVC / Remoting):

  • Web: It provides various web features such as multipart file upload, servlet listeners, and so on.
  • Web Socket: It provides two-way communication between the client and the server.
  • Portlet: It supports MVC implementation to work in a Portlet environment.

Miscellaneous:

  • AOP: It provides aspect-oriented programming.
  • Aspects: It provides integration with AspectJ.
  • Instrumentation: It provides class instrumentation and classloader implementations.
  • Messaging: It provides annotation programming for routing and processing STOMP messages.

Applications of Spring:

  • POJO (Plain Old Java Object) Based: Spring allows us to create an application using POJO. While using POJO, you do not need an EJB container.
  • Modular: Spring is modularized, so you do not need to worry about the packages you need and ignore the rest.
  • Integration with Existing Frameworks: Spring can integrate with various existing frameworks like JEE, other logging frameworks, and so on.
  • Testability: Testing an application is easy in Spring.
  • Web MVC: Spring MVC framework provides a great alternative to other frameworks such as Struts.
  • Central Exception Handling: With the help of Spring, you can easily translate technology-specific exceptions to unchecked exceptions.
  • Lightweight: Spring is beneficial for developing applications having limited memory and CPU resources.
  • Transaction Management: Spring also provides transaction management to scale down to local transactions and scale up to the global transaction.

Example:

Step 1: Create the class

public class Student {  
private String name;  
  
public String getName() {  
    return name;  
}  
  
public void setName(String name) {  
    this.name = name;  
}  
  
public void displayInfo(){  
    System.out.println("Hello: "+name);  
}  
} 

Step 2: Create the XML file

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
<bean id="studentbean" class="Student">  
<property name="name" value="ABC"></property>  
</bean>  
  
</beans> 

Step 3: Create the test class

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
  
public class Test {  
public static void main(String[] args) {  
    Resource resource=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  
      
    Student student=(Student)factory.getBean("studentbean");  
    student.displayInfo();  
}  
} 

Step 4: Load the jar files named as

  • org.springframework.core-3.0.1.RELEASE-A
  • com.springsource.org.apache.commons.logging-1.1.1
  • org.springframework.beans-3.0.1.RELEASE-A

Step 5: Run the test class.

Facebook Comments
Tags

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button
Close
Close