Sep 17, 2021
Updated: Sep 30, 2021
Hi Everyone, Today we discuss spring boot annotations.
Spring Boot Annotations is a form of metadata that provides data about a program. In other words, annotations are used to provide supplemental information about a program. It is not a part of the application that we develop. 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.
We use this annotation to mark the main class of a Spring Boot application:
@SpringBootApplicationclass VehicleFactoryApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleFactoryApplication.class, args);
}
}
@SpringBootApplication = @Configuration + @EnableAutoConfiguration +@ComponentScan annotations
It applies to the bean setter method. It indicates that the annotated bean must be populated at configuration time with the required property, else it throws an exception BeanInitilizationException.
Spring provides annotation-based auto-wiring by providing @Autowired annotation. It is used to autowire spring bean on setter methods, instance variable, and constructor. When we use @Autowired annotation, the spring container auto-wires the bean by matching data-type.
It is a class-level annotation. The class annotated with @Configuration used by Spring Containers as a source of bean definitions.
It is used when we want to scan a package for beans. It is used with the annotation @Configuration. We can also specify the base packages to scan for Spring Components.
It is a method-level annotation. It is an alternative of XML <bean> tag. It tells the method to produce a bean to be managed by Spring Container.
@EnableAutoConfiguration, as its name says, enables auto-configuration. It means that Spring Boot looks for auto-configuration beans on its classpath and automatically applies them.
Note, that we have to use this annotation with @Configuration:
@Configuration@EnableAutoConfigurationclass VehicleFactoryConfig {}
Usually, when we write our custom auto-configurations, we want Spring to use them conditionally. We can achieve this with the annotations in this section.
We can place the annotations in this section on @Configuration classes or @Bean methods.
Using these conditions, Spring will only use the marked auto-configuration bean if the class in the annotation's argument is present/absent:
@Configuration@ConditionalOnClass(DataSource.class)class MySQLAutoconfiguration {
//...
}
We can use these annotations when we want to define conditions based on the presence or absence of a specific bean:
@Bean@ConditionalOnBean(name = "dataSource")LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// ...
}
With this annotation, we can make conditions on the values of properties:
@Bean@ConditionalOnProperty(
name = "usemysql",
havingValue = "local"
)DataSource dataSource() {
// ...
}
We can make Spring to use a definition only when a specific resource is present:
@ConditionalOnResource(resources = "classpath:mysql.properties")Properties additionalProperties() {
// ...
}
With these annotations, we can create conditions based on if the current application is or isn't a web application:
@ConditionalOnWebApplicationHealthCheckController healthCheckController() {
// ...
}
We can use this annotation in more complex situations. Spring will use the marked definition when the SpEL expression is evaluated to 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() {
//...
}