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:
This article is part of a series:
As we know Spring Boot is a brand new framework from the team at Pivotal, designed to simplify the bootstrapping and development of a new Spring application.
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.
@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.
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:
This annotation marks a class as a Configuration class for Java-based configuration.
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.
This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.
@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.
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); } }
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 { //... }
The @ConditionalOnBean and @ConditionalOnMissingBean annotations let a bean be included based on the presence or absence of specific beans.
Use when we want to define conditions based on the presence or absence of a specific bean:
@Bean @ConditionalOnBean(name = "dataSource") LocalContainerEntityManagerFactoryBean entityManagerFactory() { // ... }
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.
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() { // ... }
The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present:
@ConditionalOnResource(resources = "classpath:mysql.properties") Properties additionalProperties() { // ... }
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() { // ... }
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() { // ... }
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() { //... }