CodeGym /Courses /Module 5. Spring /Various Spring modules (Core, Data, MVC, AOP, Security, e...

Various Spring modules (Core, Data, MVC, AOP, Security, etc.)

Module 5. Spring
Level 1 , Lesson 2
Available

When we talk about Spring, we don't mean just one framework — it's a whole ecosystem. Each module solves a specific problem, which makes Spring flexible and modular. Want to work with a database? Use Spring Data. Need to build a secure REST API? Spring Security and Spring MVC have you covered.

Here are a few reasons why a modular approach is awesome:

  • Flexibility: you only use the parts of Spring you need. Why pull everything in if your project only needs Spring Core?
  • Clean code: modules help isolate functionality. This is especially important in large projects.
  • Scalability: modules let you add new features without changing existing code.

Let's break down the key Spring Framework modules.


1. Spring Core

Spring Core is the framework's core. This is where our old friend, the IoC container, lives and manages all the Spring Beans. If Spring were a superhero, its superpower would be IoC, and Core would be the heart.

Key features of Spring Core:

Example: creating a Spring Bean

  • Dependency management via DI.
  • Bean configuration via annotations (@Component, @Autowired, @Qualifier) and Java-based (@Configuration, @Bean).
  • Easier testing and component reuse.

@Component
public class HelloService {
    public String sayHello() {
        return "Hello, Spring!";
    }
}

@RestController
public class HelloController {
    private final HelloService helloService;

    @Autowired
    public HelloController(HelloService helloService) {
        this.helloService = helloService;
    }

    @GetMapping("/hello")
    public String hello() {
        return helloService.sayHello();
    }
}

Here HelloService is automatically managed by the IoC container, and its instance is injected into the controller. Magic? Nope — just Spring Core.


2. Spring Data

Spring Data makes working with databases as simple and convenient as possible. JPA, annotations, and repositories come to the rescue. That means you don't have to write tons of SQL queries. Yes, you can live pain-free now!

Key features of Spring Data:

  • ORM support (via Hibernate and JPA).
  • Automatic generation of CRUD operations via JpaRepository.
  • Data models via annotations like @Entity, @Id, @GeneratedValue.

Code example: simple entity and repository


@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    // Getters and setters
}

public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByName(String name);
}

Now you can just call findByName("John") to get all users named "John". Joy hormones kick in!


3. Spring MVC

Spring MVC follows the Model-View-Controller architecture and helps us build web applications. It's the module that handles HTTP requests, routing, and generating HTML pages.

Main components of Spring MVC:

  • Controllers: handle requests via annotations like @Controller, @RequestMapping.
  • Models: the data we pass to the view.
  • Views: HTML templates rendered with tools like Thymeleaf.

Example: creating a simple controller


@Controller
public class HomeController {
    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC!");
        return "home"; // Returns the home.html template (via Thymeleaf)
    }
}

4. Spring AOP (Aspect-Oriented Programming)

AOP is a way to inject extra functionality without touching the core code. For example, you can add logging, error handling, or transaction management in one place.

Key concepts:

  • Aspect: the extra functionality you want to add (e.g., logging).
  • Pointcut: the place in the code where aspects are applied.
  • Advice: the actual aspect logic.

Code example: logging with AOP


@Aspect
@Component
public class LoggingAspect {
    @Before("execution(* com.example.service.*.*(..))")
    public void logBeforeMethodExecution() {
        System.out.println("Method is being called...");
    }
}

When a method in com.example.service is called, you'll see the message Method is being called.... It's like a spy that records all your actions.


5. Spring Security

Spring Security takes care of web app security. It includes user authentication, access control, and a bunch of other stuff.

Key features:

  • Configuring roles and access rights via annotations.
  • Integration with a database for storing users.
  • Support for modern security mechanisms like JWT and OAuth2.

Example: protecting an endpoint


@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .anyRequest().authenticated()
            .and()
            .formLogin();
    }
}

Now only users with the ADMIN role will be able to access routes under /admin/**.


6. When and which module to use?

Module When to use Example
Spring Core Always. It's the foundation of your app Managing all dependencies and IoC
Spring Data For working with the database Creating entities and repositories
Spring MVC For building web apps and REST APIs Handling HTTP requests, routing
Spring AOP When you need to inject extra functionality Logging, transaction handling
Spring Security When authentication and authorization are needed Protecting REST APIs

Even after a quick intro to the main Spring modules, you've seen how powerful, flexible, and convenient the framework is. But remember, with great power comes great responsibility. Our goal is to learn to use Spring modules so we don't end up with an overcomplicated, bloated project!

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION