Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Spring Boot provides a variety of annotations to help developers quickly bootstrap applications, define components, and configure properties. These annotations play an essential role in reducing boilerplate code and allowing developers to focus on business logic.
Here's a rundown of some commonly used Spring Boot annotations:
@SpringBootApplication
: A convenience annotation that encompasses @SpringBootConfiguration
, @EnableAutoConfiguration
, and @ComponentScan
. This is typically used on the main application class.@Component
: Generic stereotype for any Spring-managed component.@Service
: Stereotype for service classes, holding business logic.@Repository
: Stereotype for data access components (e.g., DAOs).@Controller
: Stereotype for MVC controller classes in a web application.@RestController
: A combination of @Controller
and @ResponseBody
, which indicates that the returned data should be written directly to the HTTP response as JSON or XML.@Configuration
: Indicates that the class has @Bean
definition methods. It can be used to create Spring beans programmatically.@EnableAutoConfiguration
: Enables Spring Boot's auto-configuration mechanism.@ConfigurationProperties
: Allows binding and validation of external configurations (e.g., properties or YAML files) to a Java object.@Value
: Annotation at the field level to inject values from properties files.@RequestMapping
: Annotation for mapping web requests to specific handler methods or classes.@GetMapping
, @PostMapping
, @PutMapping
, @DeleteMapping
, etc.: Shortcuts for @RequestMapping
with specific HTTP methods.@PathVariable
: Indicates that a method parameter should be bound to a URI template variable.@RequestParam
: Indicates that a method parameter should be bound to a value of a web request parameter.@RequestBody
: Indicates that a method parameter should be bound to the body of the HTTP request.@Entity
: Specifies that the class is an entity and will have a corresponding table in the database.@Table
: Provides additional information about the entity's table.@Id
: Specifies the primary key of the entity.@GeneratedValue
: Provides generation strategies for primary keys.@Column
: Provides additional settings for entity attributes, like nullable or unique.@Aspect
: Indicates that a class is an "aspect", holding pointcuts, advices, etc.@Before
, @After
, @Around
, etc.: Annotations to denote advice types in aspect-oriented programming.@Autowired
: Indicates that a bean should be injected from the Spring context. Can be used on fields, setters, or constructors.@Qualifier
: Used along with @Autowired
to specify which bean should be injected when there are multiple candidates.@Profile
: Indicates that a bean or configuration should be active only in specific profiles.This is not an exhaustive list, as Spring Boot and its related projects offer a wide range of annotations to cater to various needs. However, these are some of the commonly used annotations that developers will encounter while working with Spring Boot.
Spring Boot @Autowired annotation:
@RestController public class MyController { @Autowired private MyService myService; }
Transaction management with @Transactional in Spring Boot:
@Transactional
: Annotate methods to enable transaction management.@Service public class MyService { @Autowired private MyRepository myRepository; @Transactional public void performTransaction() { // Transactional logic } }
Security annotations in Spring Boot:
@Secured
: Restricts access based on roles.@PreAuthorize
, @PostAuthorize
: Expression-based access control.@Secured("ROLE_USER") @PreAuthorize("hasRole('ADMIN') and authentication.name == 'admin'")
Custom annotations in Spring Boot:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CustomAnnotation { // Annotation attributes }