Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaSpring Boot – Annotations

Spring Boot – Annotations

Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Following are some of the features of Spring Boot:

  • It allows avoiding heavy configuration of XML which is present in spring
  • It provides easy maintenance and creation of REST endpoints
  • It includes embedded Tomcat-server
  • Deployment is very easy, war and jar files can be easily deployed in the tomcat server

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. So in this article, we are going to discuss annotations that are available in Spring Boot with examples.

Spring Boot Annotations

Spring annotations present in the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages are commonly known as Spring Boot annotations. Some of the annotations that are available in this category are:

  • @SpringBootApplication
  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan
  • Auto-Configuration Conditions
    • @ConditionalOnClass, and @ConditionalOnMissingClass
    • @ConditionalOnBean, and @ConditionalOnMissingBean
    • @ConditionalOnProperty
    • @ConditionalOnResource
    • @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
    • @ConditionalExpression
    • @Conditional

1. @SpringBootApplication Annotation

This annotation is used to mark the main class of a Spring Boot application. It encapsulates @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.

Example:

Java




@SpringBootApplication
  
// Class
public class DemoApplication {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        SpringApplication.run(DemoApplication.class, args);
    }
}


2. @SpringBootConfiguration Annotation

It is a class-level annotation that is part of the Spring Boot framework. It implies that a class provides Spring Boot application configuration. It can be used as an alternative to Spring’s standard @Configuration annotation so that configuration can be found automatically. Most Spring Boot Applications use @SpringBootConfiguration via @SpringBootApplication. If an application uses @SpringBootApplication, it is already using @SpringBootConfiguration.

Example:

Java




@SpringBootConfiguration
public class Application {
  
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
  
    @Bean
    public StudentService studentService() {
        return new StudentServiceImpl();
    }
}


3. @EnableAutoConfiguration Annotation

This annotation auto-configures the beans that are present in the classpath. It simplifies the developer’s work by assuming the required beans from the classpath and configure it to run the application. This annotation is part of the spring boot framework. For example, when we illustrate the spring-boot-starter-web dependency in the classpath, Spring boot auto-configures Tomcat, and Spring MVC. The package of the class declaring the @EnableAutoConfiguration annotation is considered as the default. Therefore, we need to apply the @EnableAutoConfiguration annotation in the root package so that every sub-packages and class can be examined.

Example:

Java




@Configuration
@EnableAutoConfiguration
public class Application {
  
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
  
}


4. @ComponentScan Annotation

@ComponentScan tells Spring in which packages you have annotated classes that should be managed by Spring. So, for example, if you have a class annotated with @Controller which is in a package that is not scanned by Spring, you will not be able to use it as a Spring controller. So we can say @ComponentScan enables Spring to scan for things like configurations, controllers, services, and other components that are defined. Generally, @ComponentScan annotation is used with @Configuration annotation to specify the package for Spring to scan for components.

Example:

Java




@Configuration
@ComponentScan
  
// Main class
public class Application {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        SpringApplication.run(Application.class, args);
    }
}


5. @ConditionalOnClass Annotation and @ConditionalOnMissingClass Annotation

@ConditionalOnClass Annotation used to mark auto-configuration bean if the class in the annotation’s argument is present/absent.

Example:

Java




@Configuration
@ConditionalOnClass(MongoDBService.class)
  
class MongoDBConfiguration {
    // Insert code here
}


6. @ConditionalOnBean Annotation and @ConditionalOnMissingBean Annotation 

These annotations are used to let a bean be included based on the presence or absence of specific beans.

Example:

Java




@Bean
@ConditionalOnMissingBean(type = "JpaTransactionManager")
  
JpaTransactionManager jpaTransactionManager(
    EntityManagerFactory entityManagerFactory)
{
    // Insert code here
}


7. @ConditionalOnProperty Annotation 

These annotations are used to let configuration be included based on the presence and value of a Spring Environment property.

Example:

Java




@Bean
@ConditionalOnProperty(name = "usemongodb",
                       havingValue = "local")
  
DataSource
dataSource()
{
    // Insert code here
}
  
@Bean
@ConditionalOnProperty(name = "usemongodb",
                       havingValue = "prod")
  
DataSource
dataSource()
{
    // Insert code here
}


8. @ConditionalOnResource Annotation 

These annotations are used to let configuration be included only when a specific resource is present in the classpath.

Example:

Java




@ConditionalOnResource(resources
                       = "classpath:mongodb.properties")
  
Properties
additionalProperties()
{
    // Insert code here
}


9. @ConditionalOnExpression Annotation 

These annotations are used to let configuration be included based on the result of a SpEL (Spring Expression Language) expression. 

SpEL (Spring Expression Language): It is a powerful expression language that supports querying and manipulating an object graph at runtime. 

Example:

Java




@Bean
@ConditionalOnExpression("${env} && ${havingValue == 'local'}")
  
DataSource dataSource() 
{
    // Insert code here
}


10. @ConditionalOnCloudPlatform Annotation 

These annotations are used to let configuration be included when the specified cloud platform is active.

Example:

Java




@Configuration
@ConditionalOnCloudPlatform(CloudPlatform.CLOUD_FOUNDRY)
  
public class CloudConfigurationExample 
{
  // Insert code here
}


Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments