Category: Spring Boot
-
Creating a Custom Validator (Annotation) in Spring Boot
In Spring Boot applications, you can use custom validation annotations when the built-in validation annotations (like @NotNull, @Size, @Email, etc.) are not sufficient for your business logic. This tutorial explains how to create a custom annotation and validator step by step. 1. Use Case Example Suppose you have a User Registration API and you want…
-
Swagger with Spring Boot
1. Introduction In this tutorial, we’ll discuss how to add Swagger to the Spring Boot. 2. Add Swagger Dependency In pom.xml, add: 3. Access Swagger UI After starting your app, open in browser: 4. Annotations In a Spring Boot application using Springdoc OpenAPI (Swagger), you can use a variety of OpenAPI annotations to document your…
-
@WithMockUser Annotation in Spring Boot
1. Introduction The @WithMockUser annotation is part of Spring Security’s testing framework. It allows you to simulate an authenticated user in a test method without actually going through the login process. This is especially useful when writing unit or integration tests for secured endpoints or service methods protected by Spring Security. 2. Maven Dependency Make…
-
@Import Annotation in Spring Boot
1. Introduction The @Import annotation is used in Spring to import Java configuration classes, regular component classes, or even bean definition classes into the current application context. It allows for modularizing your application configuration and managing bean definitions across multiple configuration files or components. 2. Basic Definition 3. Use Cases of @Import Use Case Description…
-
@Scope annotation
1. Introduction The @Scope annotation in Spring Boot (and Spring Framework in general) is used to define the scope of a Spring bean — that is, how and when a new instance of the bean should be created and returned by the Spring container. By default, all Spring beans are singleton-scoped, meaning only one instance…
-
@ConditionalOnClass in Spring Boot
1. What is @ConditionalOnClass in Spring Boot? The @ConditionalOnClass annotation is used to conditionally register a bean or configuration in Spring Boot only if a specific class (or classes) is present on the classpath. It is part of Spring Boot’s conditional annotations family and is commonly used in auto-configuration classes or modular application design. 2.…
-
@ConditionalOnProperty annotation in Spring Boot
1. Introduction to @ConditionalOnProperty In Spring Boot, the @ConditionalOnProperty annotation is used to conditionally enable or disable a bean registration based on the presence and value of a property in the application’s configuration files (like application.properties or application.yml). It is part of the Spring Boot auto-configuration mechanism and is mainly used to write conditional logic…
-
@CachePut Annotation
1. Introduction The @CachePut annotation in Spring Framework is used to update the cache without interfering with the method execution. Unlike @Cacheable, which may skip the method execution if the value is already in the cache, @CachePut always executes the method and updates the cache with the result. 2. Use Case Use @CachePut when: 3.…
-
@Valid annotation
1. What is @Valid in Spring Boot? The @Valid annotation is part of the Java Bean Validation (JSR-380) standard, implemented by libraries like Hibernate Validator. It is used to trigger validation on a Java object’s fields based on annotations such as @NotNull, @Size, @Email, etc. 2. Dependencies Required Spring Boot starter automatically includes Hibernate Validator…
-
@Resource Annotation in Spring
1. Introduction The @Resource annotation is part of the Java EE (now Jakarta EE) standard. It allows Spring to perform dependency injection similar to @Autowired, but with slightly different behavior—especially in how beans are resolved. 2. What is @Resource? Import Statement 3. Behavior of @Resource Spring handles @Resource by name first, and by type second…
