In this quick article, we will discuss Spring boot annotations with examples.
All the Spring boot annotations are mainly from autoconfigure packages so we’ll explore the Spring Boot annotations from the below packages:
- org.springframework.boot.autoconfigure
- org.springframework.boot.autoconfigure.condition
This article is part of a series:
The framework takes an opinionated approach to configuration, freeing developers from the need to define a boilerplate configuration. It provides defaults for code and annotation configuration to quick start new Spring projects within no time.
Let's list all the annotations from these packages.
1. @SpringBootApplication
@SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.
@SpringBootApplication Annotation Example
We use this annotation to mark the main class of a Spring Boot application:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }Basically, the @SpringBootApplication annotation is a combination of the following three Spring annotations:
@Configuration
This annotation marks a class as a Configuration class for Java-based configuration.@ComponentScan
This annotation enables component-scanning so that the web controller classes and other components you create will be automatically discovered and registered as beans in Spring's Application Context.All the @Component, @Service, @Repository, and @Controller annotated classes are automatically discovered by this annotation.
@EnableAutoConfiguration
This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.Read more about @SpringBootApplication annotation on Spring Boot @SpringBootApplication Annotation with Example.
2. @EnableAutoConfiguration
@EnableAutoConfiguration annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added.
Since the spring-boot-starter-web dependency added to classpath leads to configuring Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
When using @SpringBootApplication annotation, the auto-configuration of the context is automatically enabled, and adding this annotation has, therefore, no additional effect.
@EnableAutoConfiguration Annotation Example
Let's add @EnableAutoConfiguration annotation to the Spring Boot Application class or Main class to enable an auto-configuration feature.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Read more about @EnableAutoConfiguration annotation on Spring Boot @EnableAutoConfiguration Annotation with Example
3. @ConditionalOnClass and @ConditionalOnMissingClass
These annotations belong to Class conditions. The @ConditionalOnClass and @ConditionalOnMissingClass annotations let configuration be included based on the presence or absence of specific classes.
Example: In the below example, using these conditions, Spring Boot will only use the marked auto-configuration bean if the class in the annotation’s argument is present/absent:
@Configuration
@ConditionalOnClass(DataSource.class)
class OracleAutoconfiguration {
//...
}
4. @ConditionalOnBean and @ConditionalOnMissingBean
The @ConditionalOnBean and @ConditionalOnMissingBean annotations let a bean be included based on the presence or absence of specific beans.@ConditionalOnBean Annotation Example
Use when we want to define conditions based on the presence or absence of a specific bean:
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// ...
}
@ConditionalOnMissingBean Annotation Example
When placed on a @Bean method, the target type defaults to the return type of the method, as shown in the following example:
@Configuration
public class MyAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService() { ... }
}
In the preceding example, the myService bean is going to be created if no bean of type MyService is already contained in the ApplicationContext.
5. @ConditionalOnProperty
The @ConditionalOnProperty annotation lets configuration be included based on a Spring Environment property.@ConditionalOnProperty Annotation Example
With this annotation, we can make conditions on the values of properties:
@Bean
@ConditionalOnProperty(
name = "usemysql",
havingValue = "local"
)
DataSource dataSource() {
// ...
}
6. @ConditionalOnResource
The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present:@ConditionalOnResource(resources = "classpath:mysql.properties")
Properties additionalProperties() {
// ...
}
7. @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
The @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a “web application”.A web application is an application that uses a Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.
@ConditionalOnWebApplication annotation sample code
With these annotations, we can create conditions based on if the current application is or isn’t a web application:
@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
// ...
}
8. @ConditionalExpression
We can use this annotation in more complex situations. Spring will use the marked definition when the SpEL expression is evaluated to be true:@Bean
@ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
DataSource dataSource() {
// ...
}
9. @Conditional
For even more complex conditions, we can create a class evaluating the custom condition.We tell Spring to use this custom condition with @Conditional:
@Conditional(HibernateCondition.class)
Properties additionalProperties() {
//...
}
Learn complete Spring Boot on Spring Boot Tutorial
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment