Comprehensive List of Spring Boot Annotations and Attributes
- Mark Kendall
- Mar 14
- 2 min read
Alright, let's consolidate all the annotations and attributes we've discussed into a comprehensive, single-article list. This will cover entities, repositories, services, controllers, configurations, and Spring Data REST.
Comprehensive List of Spring Boot Annotations and Attributes
I. Core Spring Boot Components
@SpringBootApplication:
Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Marks the main application class.
@Configuration:
Indicates that a class provides bean definitions.
@Bean:
Defines a bean within a configuration class.
@Component:
A generic stereotype for any Spring-managed component.
@Service:
Indicates that a class is a service component (business logic).
@Repository:
Indicates that a class is a repository component (data access).
provides exception translation.
@RestController:
Indicates that a class is a REST controller.
@Controller:
Indicates that a class is a web controller.
@Autowired:
Injects dependencies.
@Value("${property.name}"):
Injects property values from configuration files.
@PropertySource("classpath:application.properties"):
Loads properties from a file.
II. Entities (JPA)
@Entity:
Marks a class as a JPA entity.
@Table(name = "table_name"):
Specifies the database table name.
@Id:
Marks a field as the primary key.
@GeneratedValue(strategy = GenerationType.IDENTITY):
Specifies how the primary key is generated (e.g., auto-increment).
@Column(name = "column_name"):
Maps a field to a database column.
@OneToMany, @ManyToOne, @OneToOne, @ManyToMany:
Define relationships between entities.
@Temporal(TemporalType.TIMESTAMP) or @Temporal(TemporalType.DATE):
Define date/time attributes.
Validation Constraints (from javax.validation.constraints):
@NotNull, @Size, @Pattern.
III. Repositories (Spring Data)
JpaRepository<T, ID>:
Provides common CRUD operations for JPA entities.
MongoRepository, RedisRepository, CassandraRepository, ElasticsearchRepository, Neo4jRepository, JdbcRepository:
Repository interfaces for various data stores.
@Query("SQL or JPQL query"):
Allows custom queries.
@EnableJpaRepositories:
Enables JPA repositories.
IV. Controllers (REST)
@RequestMapping("/api/products"):
Maps requests to the controller.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping:
Map HTTP methods to handler methods.
@PathVariable:
Extracts path variables from the URL.
@RequestBody:
Extracts the request body.
@RequestParam:
Extracts query parameters.
@ResponseStatus(HttpStatus.CREATED):
Sets the HTTP response status.
@CrossOrigin:
Enables Cross-Origin Resource Sharing.
@ControllerAdvice:
Allows you to handle exceptions globally.
@ExceptionHandler(Exception.class):
Handles specific exceptions.
V. Services
@Transactional:
Manages transactions.
VI. Security
@EnableWebSecurity:
Enables Spring Security.
@PreAuthorize, @PostAuthorize, @Secured:
used to control access to methods.
VII. Spring Data REST
@RepositoryRestResource(collectionResourceRel = "products", path = "products"):
Customizes REST API exposure for repositories.
collectionResourceRel, customizes the rel name of the collection.
path, customizes the path of the collection.
VIII. Other Important Annotations
@EnableScheduling:
Enables scheduled tasks.
@EnableWebMvc:
Enables Spring MVC.
This comprehensive list should serve as a valuable reference for your Spring Boot development.
Comments