Lets understand the different SpringBoot Annotations and how to use them.
@RestController and @RequestMapping Annotations (spring mvc annotations)
They are Stereotype annotation and are hints for people reading the code and for Spring that the class plays some specific role.@Controller is a web controller, so Spring considers it when handling incoming web requests. @RestController is a combination of @Controller and @ResponseBody.
The @RequestMapping annotation provides “request routing” information to the spring. Any HTTP request with the / path should be mapped to the home method for example. The @RestController annotation tells Spring to display the resulting string back to the caller.
@EnableAutoConfiguration Annotation
This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly. h2database dependency, would signify the in-memory database for the auto-configuration.
@SpringBootConfiguration is one example of AutoConfiguration.
@Controller – Is a class level annotation, which says the class will serve as a controller of requests. It’s a MVC controller.
@Component – Spring can automatically scan a package for beans if component scanning is enabled. It’s a class level annotation.
@ComponentScan: It enables @Component scan on the package where the application is located. @ComponentScan configures which packages to scan for classes with annotation configuration. Its a package level Annotation.
@Configuration: allow to register extra beans in the context or import additional configuration classes
@Repository – DAO or Repository classes usually represent the database access layer in an application, and should be annotated with @Repository:
@Service – business logic of an application usually resides within the service layer – so we’ll use the @Service annotation to indicate that a class belongs to that layer:
@Service
public class OrderPizza {
// …
}
(data access
@Entity – Spring Boot tries to guess the location of your @Entity definitions, based on the @EnableAutoConfiguration it finds. To get more control, you can use the @EntityScan annotation
@EntityScan –
@Id – Field in model class as primary key in the database.
@Param – We can pass named parameters to our queries using @Param:
@Query("from Alien where p.tech = :tech order by aid")
List<Alien> findByAidSorted(@Param("tech") String tech);