This is purely going to be about Annotations I have encountered while developing my first Java Spring Boot project.
@Autowired
: 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.
@Configuration
: It is a class level annotation. The class annotated with @Configuration used by Spring Containers as a source of bean definitions.
@Bean
: 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.
@Component
: class level annotation. It is used to mark a Java class as a bean. A Java class annotated with @Component is found during the classpath. The Spring Framework pick it up and configure it in the application context as a Spring Bean
.
@Controller
: Class level annotation. Specialization of @Component. It makes a class as a web request handler. It is often used to serve web pages. By default, it returns a string that indicates which route to redirect. It is mostly used with @RequestMapping annotation.
@Service
: Class Level annotation. Tells the Spring that class contains the business logic.
@Repository
: Class level annotation. It is a Data Access Object (DAO) that access the database directly. The repos does all the operations related to the database.
@SpringBootApplication
: Combination of three annotations
@GetMapping
: Maps the HTTP GET request on the specific handler method.
Used to create a web service endpoint that fetches. used instead of @RequestMapping(method=RequestMethod.GET)
Same goes for 'PUT','POST','DELETE'
@RequestBody
: used to bind HTTP request with an object in a method parameter. Internally it uses HTTP MessageConverters to convert the body of the request. The Spring framework binds the incoming HTTP request body to that parameter.
@ResponseBody
: binds the method return value to the response body. It tells the Spring Boot Framework to serialize a return an object into JSON and XML format.
@PathVariable
:used to extract the values from the URL. Most suitable for the REST web service where the URL contains a path variable.
@RequestParam
: used to extract the query parameters from the URL. It can specify default values if the query parameter is not present in the URL.