Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
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:
@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.
@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.
@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.
@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.
@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.
@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.
Overview of Spring Core annotations:
@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 }
Commonly used annotations in Spring:
@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;
Annotations for dependency injection in Spring:
@Autowired
and @Qualifier
facilitate the injection of dependencies into Spring beans.@Autowired private MyDependency myDependency; @Qualifier("specificBean") private MyBean myBean;
Spring MVC annotations for web development:
@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"; } }
Transaction management annotations in Spring:
@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 }
Security-related annotations in Spring framework:
@Secured
, @PreAuthorize
, and @PostAuthorize
are used for securing methods and controlling access.@Secured("ROLE_USER") public void secureMethod() { // Secured method logic }