DEV Community

Hamza Salih-Eddine
Hamza Salih-Eddine

Posted on

Understanding Common Spring Boot, Spring Security, and JPA Annotations

Recently, I started developing a full-stack web application using Spring Boot for the backend and Angular for the frontend. You can check out the project on my GitHub repo to see it in action.

While building the app, I realized how much Spring Boot and its ecosystem simplify development with annotations. Since there are so many annotations, I won’t cover all of them. Instead, I’ll focus on the commonly used ones, explaining their purpose and why they’re useful.

Spring Boot Core Annotations

@SpringBootApplication:

Marks the main class of a Spring Boot application. It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan to simplify setup.

@Configuration:

Marks a class as a source of Spring bean definitions. Methods annotated with Bean inside it define beans managed by the Spring context.

@Bean:

Declares a bean method whose return value is managed by Spring. Commonly used for third-party library classes since you cannot annotate external classes with @Component.

@Component:

Marks a class as a Spring-managed component that can be automatically detected via classpath scanning.

@Service:

A specialized @Component for service-layer classes. It indicates that the class contains business logic. While it does not enforce a singleton by itself, Spring by default manages beans as singletons, ensuring a single instance per application context.

@RestController:

Combines @Controller and @ResponseBody to automatically serialize/deserialize JSON request and response bodies. Used for building REST APIs.

Spring Security Annotation

@EnableWebSecurity:

Enables Spring Security in the application and allows customizing authentication and authorization rules. Typically used in a configuration class that defines a SecurityFilterChain bean.

JPA / Hibernate Annotations

@Entity:

Marks a class as a database entity. Hibernate detects it at bootstrap and maps it to a database table.

@Table(name = "comment"):

Specifies the database table name for the entity. If omitted, the table name defaults to the entity class name.

@Id:

Marks the primary key field of the entity.

@GeneratedValue:

Configures automatic generation of primary key values using strategies like IDENTITY or SEQUENCE.

@CreationTimestamp:

Automatically sets the timestamp when the entity is first persisted.

@ManyToOne:

Defines a many-to-one relationship between entities.

@OneToMany:

Defines a one-to-many relationship. Typically paired with mappedBy to indicate the owning side.

@ManyToMany:

Defines a many-to-many relationship between entities, often with a join table.

@JoinColumn:

Specifies the foreign key column used in a relationship.

@Transactional:

Applied to service methods or classes to ensure database transactions roll back automatically if an error occurs during execution.

Other Useful Annotations

@Column:

Customize column name, type, and constraints.

@Transient:

Marks fields that should not be persisted to the database.

@Enumerated:

Maps enums to database columns (e.g., as STRING or ORDINAL).

@Embedded / @Embeddable:

Used for reusable embedded objects within an entity.

Conclusion

Annotations are one of the features that make Spring Boot, Spring Security, and JPA so powerful and developer-friendly. They help reduce boilerplate code, improve readability, and make your application easier to maintain.

I hope this guide gave you a clear understanding of the commonly used annotations and when to apply them in your projects.

Thanks for taking the time to read this blog! If you found it helpful, feel free to share your thoughts and questions in the comments.

Top comments (0)