Spring 2.x modules - bhudevi0924/Spring-boot GitHub Wiki

Spring 2.x modules:

  • Spring MVC: A powerful web framework based on the Model-View-Controller design pattern. => Example using Spring MVC. 1) Setup: Add dependency in the configuration file.

              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>2.5.6</version> <!-- or the version you're using -->
              </dependency>
    
    2) Controller: Create a controller to handle HTTP requests.
    
              package com.example.controller;
    
              import org.springframework.stereotype.Controller;
              import org.springframework.web.bind.annotation.RequestMapping;
              import org.springframework.web.bind.annotation.RequestMethod;
    
              @Controller
              public class HelloController {
    
                  @RequestMapping(value = "/hello", method = RequestMethod.GET)
                  public String hello() {
                  return "hello";
                  }
              }
    
    3) Create Spring Configuration: Create a Spring configuration file, typically dispatcher-servlet.xml (or any name ending with -servlet.xml). This 
       file will configure the Spring MVC components like controllers, view resolver, etc.
              
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:mvc="http://www.springframework.org/schema/mvc"
              xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-2.5.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-2.5.xsd">
    
              <!-- Enable component scanning for Spring MVC controllers -->
              <context:component-scan base-package="com.example.controller" />
    
              <!-- Configure the view resolver -->
              <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="prefix" value="/WEB-INF/views/" />
                  <property name="suffix" value=".jsp" />
              </bean>
    
              <!-- Enable Spring MVC annotations -->
              <mvc:annotation-driven />
          </beans>
     
    4) Create View: Create a JSP file to serve as the view. (File named hello.jsp under /WEB-INF/views/).
              <!-- /WEB-INF/views/hello.jsp -->
              <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
              <!DOCTYPE html>
              <html>
                  <head>
                      <title>Hello World</title>
                  </head>
                  <body>
                      <h1>Hello, Spring MVC!</h1>
                  </body>
              </html>
    
  • Java 5 features: Added support for Java 5 features such as generics, annotations, etc. 1) Generics: Generics in Java provide a way to create classes, interfaces, and methods that operate on a variety of types while providing compile- time type safety. public class Box { private T value;

               public void setValue(T value) {
                   this.value = value;
               }
    
               public T getValue() {
                   return value;
               }
           }
    
    2) Annotations : @Autowired, @Controller, @Service, @Repository etc...
    3) Enhanced for loop: Use enhanced for loops to iterate over collections retrieved from Spring-managed beans or other data sources. (foreach loop)            
    4) Varargs: Allow methods to accept a variable number of arguments. The vararg parameter has to be the last parameter in the method signature.
          
               public void myMethod(String... strings) {
                    // Method body
               }
               myMethod("apple", "banana", "orange");
               myMethod(); // Valid, with an empty array passed implicitly
    
    
    5) Static imports : Allow to access static members (fields and methods) of classes directly without qualifying them with the class name. This 
       feature simplifies the usage of static members and improves code readability.
            import static java.lang.Math.PI;
            import static java.lang.Math.sqrt;
    
  • Enhanced AOP: Improved support for Aspect-Oriented Programming.

  • JDBC abstraction: Enhanced JDBC support with features like NamedParameterJdbcTemplate.

  • JMS (Java Message Service) support: Integration with JMS for messaging. -> This module allows to build messaging applications using JMS providers such as Apache ActiveMQ, IBM MQ, or RabbitMQ.

⚠️ **GitHub.com Fallback** ⚠️