Spring Boot Annotations

Spring Boot Annotations

  • Atualizado
  • Publicado em Spring Boot
  • 3 minutos de leitura

Spring Boot Annotations

Working with Spring Boot feels like having a magical toolbox: it simplifies configuration so much that all we often need to do is use the right annotation. Today, I want to explore some of the most important annotations, particularly from the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages. These annotations make developers’ lives much easier by automating many configuration tasks.

1. @SpringBootApplication

This is the primary annotation to mark the entry class of your Spring Boot application. Behind the scenes, it encapsulates three other annotations:

  • @Configuration: Indicates that the class can define beans for the Spring context.
  • @EnableAutoConfiguration: Enables auto-configuration, which automatically detects beans in the classpath and configures them.
  • @ComponentScan: Scans packages to find components and register them in the context.

@SpringBootApplication
class VehicleFactoryApplication {

public static void main(String[] args) {
SpringApplication.run(VehicleFactoryApplication.class, args);
}
}

This annotation is a great example of how Spring Boot reduces manual work and organizes everything in one place.

@EnableAutoConfiguration

If we want to use auto-configuration without the convenience of @SpringBootApplication, we can use @EnableAutoConfiguration manually alongside @Configuration. This allows Spring Boot to configure beans automatically based on the resources found in the classpath:

@Configuration
@EnableAutoConfiguration
class VehicleFactoryConfig {}

Auto-Configuration Conditions

For scenarios where we need more customized configurations, Spring Boot gives us the flexibility to enable or disable configurations based on specific conditions. Here are the main annotations used:

@ConditionalOnClass and @ConditionalOnMissingClass

These annotations check whether a specific class is present or absent in the classpath. They’re useful for configuring beans that depend on external libraries.

@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoconfiguration {
//…
}

@ConditionalOnBean and @ConditionalOnMissingBean

These check for the presence or absence of a specific bean in the context.

@Bean
@ConditionalOnBean(name = “dataSource”)
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// …
}

@ConditionalOnProperty

This allows conditional configuration based on properties defined in the configuration file (application.properties or application.yml).

@Bean
@ConditionalOnProperty(
name = “usemysql”,
havingValue = “local”
)
DataSource dataSource() {
// …
}

@ConditionalOnResource

Here, we can condition the configuration on the presence of a specific resource in the classpath.

@ConditionalOnResource(resources = “classpath:mysql.properties”)
Properties additionalProperties() {
// …
}

@ConditionalOnWebApplication and @ConditionalOnNotWebApplication

These allow configuring beans based on whether the current application is a web or non-web application.

@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
// …
}

@ConditionalOnExpression

Ideal for more complex conditions, evaluated with SpEL (Spring Expression Language).

@Bean
@ConditionalOnExpression(“${usemysql} && ${mysqlserver == ‘local’}”)
DataSource dataSource() {
// …
}

@Conditional

Last but not least, we can create our own custom conditions. This gives us full control over when a configuration should be applied.

@Conditional(HibernateCondition.class)
Properties additionalProperties() {
//…
}

These annotations give us an incredible level of flexibility and control when developing Spring Boot applications. I hope this guide helps you better understand how and when to use these powerful tools!

Deixe um comentário