Skip to the content.

Spring > Modules

Overview

Spring is made up of different modules that provide specialized functionalities for various application needs. Here’s an overview of key Spring modules:

1. Spring Core

2. Spring Boot

3. Spring Web

Spring supports development of web applications and RESTful APIs.

Example of a REST Controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Spring!";
    }
}

4. Spring Data

Spring provides seamless integration with various data access technologies:

Example of Spring JDBC

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    private final JdbcTemplate jdbcTemplate;
    
    public UserRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public void saveUser(String name) {
        jdbcTemplate.update("INSERT INTO users(name) VALUES (?)", name);
    }
}

5. Spring Messaging

Spring Messaging provides integration with messaging platforms like JMS and Kafka.

Example of Spring Kafka

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaConsumerService {
    @KafkaListener(topics = "test-topic", groupId = "group_id")
    public void consume(String message) {
        System.out.println("Received message: " + message);
    }
}

6. Spring Security

Spring Security provides authentication, authorization, and protects applications against security threats like CSRF and XSS

Example of Spring Security Configuration

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and().httpBasic();
    }
}

7. Spring AOP (Aspect-Oriented Programming)

Spring AOP helps separate cross-cutting concerns from business logic.

Example of AOP in Spring

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    @Before("execution(* com.example.service.*.*(..))")
    public void logBeforeMethod() {
        System.out.println("Logging before method execution");
    }
}

8. Spring Cloud

9. Spring Batch

Spring’s modular architecture allows developers to pick only the components they need, making it highly flexible and scalable.


← Spring Architecture Spring Beans →

🔗 Related Topics: