How to use some Basic Spring Boot Annotations
Spring Boot enables us to produce independent, productive applications based on Spring with less effort. Currently, it does not use XML designs because of the use of Annotations in the Spring Boot, it implements the design rules.
Annotation is a type of metadata that includes program information that is not part of the program itself, similar to commentary. The code functions are not affected by the notes, they simply comment or annotate.
Some Basic Spring Boot Annotations
@Configuration is a duplicate file for the XML settings. It is used in classes with boobs. Java class has commented on @Configuration, an independent design with methods to represent and build needs.
- @Required
For the bean setter methods, the @Required annotation is used. The desired property will be used if we want to apply it. This annotation shows that the beam must be occupied with the property needed at design time. If there is not a type of BeanIntialisationException thrown.
- @Bean
@Bean is a method level annotation used. @Bean annotation functions for Spring beans with @Configuration. In both the indications and the design of needs, @Configuration will have methods. Such methods are annotated with @Bean. The procedure annotated with this comment functions as a bean identification and returns its designs and the true bean.
- @EnableAutoConfiguration
This annotation is generally established in the main application class. This annotation defines the “search package.” This annotation notifies Spring Boot to start adding beans according to the classpath controls, other beans and various functional parameters.
- @SpringBootApplication
The @SpringBootApplication annotation in the application class is used when you start a Spring Boot project. In the base package, the class with @SpringBootApplication annotation is required. @SpringBootApplication Annotation scans the parts but only scans its parts. @SpringBootApplication We keep the class noted under @SpringBootApplication in the ‘sample.example’ and then scans the entire sub-package of it under @SpringBootApplication like sample.examples.a and sample.examples.b.
Annotation of @SpringBootApplication is appropriate and adds @ComponentScan,@EnableAutoConfigure and @Configuration.
- @ComponentScan
As above, with @Configuration, this annotation enables Spring to view packages, scan for annotated elements. We can also define the base packages with the aid of this annotation by using the functionality ‘basePacket’ or ‘basePackageClasses.’ If the individual packages are not specified, the class package declared by this annotation is used to scan.
- @Autowired
@Autowired is used for setter, building and field methods. This involves inserting dependence on the object. If you use @Autowired in fields and give the values for fields with the name of the property, then the passed values are automatically assigned to the fields by Spring. @Autowired can be used for private properties as well.
- @Qualifier
@Qualifier works with @Autowired in partnership. If we want the dependency insertion action to add power, this annotation is applied. This can be defined as single builder arguments or variables of the method.
If we make more beans of a similar kind and want to wire just one feature, this annotation is used to ignore insecurity.
- @Lazy
This annotation is used by the component classes. All ‘automatic’ dependencies are designed and arranged by default during the start-up. If we want to load a bean slowly, use @Lazy above the class. This suggests that the bean is only designed and loaded when requested initially.
- @Value
@Value will be used in the parameter level of the builder, field, and method. This is a default value statement for the property loading parameter or field.
- @Service
This annotation is used by Class. This indicates a Java class, which performs calculations, commercial logic and calls external APIs for certain services. This is a specially designed annotation for the @Component annotation used in the service layer.
- @Repository
This annotation is used by Java classes accessing the database directly. It works as an indicator for any class, repository or data access object, that complies with it.
@Repository has the property of automated translation. For example, in the @Repository if an exception is made, a handler is available and there is no need to attach a try-catch block.
Summary
Thank you so much for taking the time to read this. I hope you now have a better understanding of spring-boot annotations and that you enjoyed reading it as much as I enjoyed writing it.