Spring Framework

The Spring framework is an open-source and the most popular java platform application framework. Rod Johnson developed it in June 2003 for Java EE development. Over the years, the developers have released various versions of the Spring framework to support other frameworks and to include various modules, such as IOC and ORM, making it the framework of frameworks.  The developers use this framework to create lightweight applications with less and reusable code with high performance. Moreover, they can use its extensions to build applications on top of the Java EE platform to streamline the development of server-side applications, REST APIs, and other systems.

Application Development using Spring web framework in Java

Benefits of Using Spring Framework for Development

Below are the benefits for the developers to use the Spring web framework for their application development:

  1. The Spring framework is relatively faster because of its optimized code execution, support of non-blocking reactive programming, model, and faster start-up and shutdown.
  2. Due to its advanced design, the Spring framework is an excellent alternative to other MVC or non-MVC web frameworks, such as Struts.
  3. The developers can only focus on the dependencies and packages they need and ignore the other existing packages without modifying them because of their modular organization.
  4. It is easier and simpler for the developers to test the Spring web applications because the code is lesser and more straightforward, and the framework handles the dependencies itself. Moreover, injecting testing data using JavaBeanstyle POJOs (Plain Old Java Objects) is easier.
  5. The users do not need to learn the technology from scratch because the framework utilizes existing technologies such as JDK times, several logging, and ORM framework in its framework.
  6. The Spring framework can conveniently translate the technology-specific exceptions, for instance, SQL and hibernate exceptions, to its consistent exception class.
  7. Spring framework is secure because it monitors the third-party dependencies closely, and the users can easily integrate modern industry-standard security schemes into it. Moreover, security professionals review its code commits before releasing them; therefore, its solutions are secure by default.
  8. The developers can use POJOs(Plain Old Java Objects) to develop their applications, enabling them to use only a servlet container, for instance, Tomcat.
  9. The applications are lighter in weight which is extremely beneficial in the low-resource development environment, such as limited memory.
  10. The framework allows scaling up and down the translation management interface it provides to its users.
  11. It has a diverse community of developers worldwide; therefore, the users can easily find tutorials, peers, guidelines, and training sessions to support and help them. Moreover, developers can get certifications to help them in their professional careers.
  12. The Spring framework allows its developers to include third-party libraries and extensions for their application development, making it a flexible framework.

Application Development using Spring Web Framework

This tutorial uses the Intellij IDEA for application development using the Spring web framework. However, the users can use any IDE, such as Eclipse, Netbeans, and visual studio, for their application development. The steps below show the users how to accelerate their application development using this framework.

  1. Start the IntelliJ IDEA, create a new project, and start with the Spring initializer to include the dependencies quickly. Moreover, it does most of the setup for development.
  2. Use default start.spring.io as a status service URL. Name the project, and select the JDK available JDK version.
  3. Select “spring web” dependency in the dependency section and version 2.3.
  4. After completing the launching steps, the application launches the user’s new project. Now, the user can see the included files and the setup to start his development.
  5. The user needs to add a REST controller to the “com.example.project web application” package by creating a new class inside this package. In this example, name the controller as “simple controller.”
  6. Add the rest controller method to the controller class that returns the “Hello world.” The request mapping maps the route and points to the index methods. When invoked from a browser or using ‘curl’ on the command line, the method returns pure text because the rest controller combines controller and response by two annotations that result in web requests returning data rather than view.
    @restController 
    public class simpleController { 
    @RequestMapping("/") 
        public String display() 
        { 
            return "Hello World"; 
        }    
    }
  7. In the above code, The rest controller is the advanced version of the spring controller, which does not require a response body and automatically serializes the return objects, thus making the implementation of these controllers easier for the users. Moreover, the request mapping annotation in the above code maps the HTTP request to the REST controllers.
  8. Open the existing application class named “application.java.” The spring initializer creates a basic class for the developers, and they can then modify it according to their needs.
    @bean
    
    public void setApplicationContext(ApplicationContext ctx) {
               return args -> {
              String a = ctx.getBean()
    String[] beanNames = ctx.getBeanDefinitionNames();
                    Arrays.sort(beanNames);
                    for (String beanName : beanNames) {
                          System.out.println(beanName);
                    }
    }; 
    
        }
  9. In the above code, spring bean annotation specifies that this method will return a bean for the Spring context. The bean gets all the beans the application creates, sorts their names, and prints them on the user’s screen.
  10. The users should also notice that the “application.java” class contains the main method. This method calls the Spring’s SpringApplication.run() method to launch/run the user’s application without needing to do any explicit configurations.
  11. Paste the command below in the terminal to run the application:
    ./gradlew bootRun
  12. The users can test the above end-point they have created by adding the following dependency to the “build.gradle” file.
    testImplementation('org.springframework.boot:spring-boot-starter-test')
  13. In the “com.example.springboot” package, create a “simpleControllerTest.java” class to mock the request and response through the above endpoint using the below code:
@SpringBootTest
@AutoConfigureMockMvc
public class simpleControllerTest {
     @Autowired
     private MockMvc mvc;

     @Test
     public void getHelloWorld() throws Exception {
          mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                      .andExpect(status().isOk())
                      .andExpect(content().string(equalTo("Hello World")))
     }
}

In the above code, “MockMVC” class represents the primary entry point for Spring test support. The annotation “autoconfigureMockMVC” auto-configures and enable the MockMVC to test the end-point. Moreover, the user should notice that the Spring boot finds the application’s main class and the endpoint by itself without the user’s need to write the path explicitly. The annotation “SprinBootTest” creates the context of the whole application to test it. However, the users can explicitly limit it to the web layer.