Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Framework Annotations

The Spring Framework provides a comprehensive set of annotations to facilitate development across its various modules, from core container functionality to web and data access layers. Below is an overview of some commonly used Spring annotations across various areas:

1. Core Container:

  • @Component: Indicates that a class is a Spring component. Spring's component scanning mechanism can pick it up and pull it into the application context.

  • @Configuration: Indicates that a class provides @Bean definitions to create beans programmatically in the Spring container.

  • @Bean: Used with @Configuration to declare a Spring bean.

  • @Scope: Specifies the scope of a bean, e.g., "singleton", "prototype", "request", "session".

  • @Autowired: Performs dependency injection by type.

  • @Qualifier: Used with @Autowired to specify which bean should be injected in case of ambiguity.

  • @Value: Used to inject values, either hardcoded strings or from property files.

  • @PropertySource: Specifies the path to a property file.

2. Aspect-Oriented Programming (AOP):

  • @Aspect: Declares a class as an aspect for AOP.

  • @Pointcut: Declares a pointcut expression.

  • @Before, @After, @Around, @AfterReturning, @AfterThrowing: Define advice types to be applied at specific join points.

3. Spring MVC:

  • @Controller: Indicates a class is a Spring MVC controller.

  • @RestController: A shorthand for @Controller + @ResponseBody, indicating that the class is a RESTful controller.

  • @RequestMapping: Maps web requests to specific handler methods. It can be specialized using @GetMapping, @PostMapping, @PutMapping, etc.

  • @RequestParam: Binds request parameters to method parameters.

  • @PathVariable: Binds the value of a URL template segment to a method parameter.

  • @ResponseBody: Indicates that the return value should be bound to the web response body.

  • @RequestBody: Indicates a method parameter should be bound to the request body.

  • @ResponseStatus: Marks a method or exception class with a status code and reason.

4. Data Access:

  • @Repository: Indicates a class is a repository (typically for DAO operations). It can translate database exceptions into Spring's DataAccessException.

  • @Transactional: Indicates that a method or class should have transactional behavior.

  • @EnableTransactionManagement: Used with @Configuration to enable Spring's annotation-driven transaction management.

5. Spring Data:

  • @Query: Defines a custom query at the method level.

6. Spring Boot:

  • @SpringBootApplication: A convenience annotation that adds all of the following: @Configuration, @EnableAutoConfiguration, and @ComponentScan.

  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, beans, and various property settings.

7. Integration:

  • @ServiceActivator, @Endpoint, @Gateway, @MessagingGateway: Annotations used for Spring Integration.

8. Testing:

  • @RunWith: Used to change the runner for tests. Commonly used with SpringRunner.class for Spring tests.

  • @SpringBootTest: Annotation that can be used for a basic integration test.

  • @MockBean: Used in combination with @SpringBootTest for Mockito mock integration.

  • @TestPropertySource: Specifies properties for the test context.

This is by no means an exhaustive list, but it provides a good overview of some of the most commonly used annotations in various areas of the Spring Framework. Depending on the specific module or project (e.g., Spring Data, Spring Cloud), you might encounter even more annotations tailored to those functionalities.

  1. Overview of Spring Core annotations:

    • Core annotations in Spring include @Component, @Repository, @Service, and @Controller. These annotations are used to declare beans and provide additional metadata to the Spring container.
    @Component
    public class MyComponent {
        // Component implementation
    }
    
  2. Commonly used annotations in Spring:

    • Other commonly used annotations include @Autowired for dependency injection, @Value for property injection, and @Qualifier for specifying the bean to be injected.
    @Autowired
    private MyDependency myDependency;
    
    @Value("${app.property}")
    private String appProperty;
    
  3. Annotations for dependency injection in Spring:

    • Dependency injection annotations such as @Autowired and @Qualifier facilitate the injection of dependencies into Spring beans.
    @Autowired
    private MyDependency myDependency;
    
    @Qualifier("specificBean")
    private MyBean myBean;
    
  4. Spring MVC annotations for web development:

    • In Spring MVC, annotations like @Controller, @RequestMapping, @RequestParam, and @ResponseBody are used to handle web requests and responses.
    @Controller
    @RequestMapping("/myController")
    public class MyController {
        @RequestMapping("/myEndpoint")
        public String handleRequest(@RequestParam String param) {
            // Controller logic
            return "viewName";
        }
    }
    
  5. Transaction management annotations in Spring:

    • Annotations like @Transactional are used for declarative transaction management, ensuring that a series of operations either succeed entirely or fail entirely.
    @Transactional
    public void performTransactionalOperation() {
        // Transactional logic
    }
    
  6. Security-related annotations in Spring framework:

    • Security annotations such as @Secured, @PreAuthorize, and @PostAuthorize are used for securing methods and controlling access.
    @Secured("ROLE_USER")
    public void secureMethod() {
        // Secured method logic
    }