Annotations in Spring, Hibernate and Springboot

  • By Pooja Nandode-Bhavsar
  • August 22, 2024
  • Uncategorized
Annotations in Spring, Hibernate and Springboot

Annotations in Spring, Hibernate and Springboot

In this blog, we are going to discuss Annotations in Spring, Hibernate and Springboot. So basically, annotations are nothing but metadata that provides extra information about the annotated class and method to the compiler and user.

 

Annotations in Spring

@Autowired

@Autowired annotation tells the spring IOC container that we are not creating the bean of this class and Spring IOC container has to create it and inject it into another class’s property on which we have used this annotation

 

@Qualifier

if we have 2 beans of the same class available to inject into another  class so in the case container creates an ambiguity that which bean user wants to inject into the dependent bean,so we can use @Qualifier annotation in which we can mention the name of that going be inject into the dependent bean

 

@Component

@Component annotation is an alternative to the <bean> tag in XML file. this stereotype @Component annotation tells the spring container that we don’t have to create this class bean in an XML file using <bean> tag. Spring IOC container will automatically create the bean of annotated class in the IOC container

@Value

@Value annotation is used to specify the value of a bean here. if we are not using @Value annotation here then we need to use setters in main() to set the values of bean properties

 

@Configuration

the class that is annotated with @Configuration  annotation can consist of a bean configuration(just like a .xml configuration file).

 

@Bean 

It is a method-level annotation that Indicates that a method generates a bean to be managed by the Spring container.

 

Annotations in Spring

@Entity

this @Entity annotation tells hibernate to map this annotated class as a table into a database with the same name as the class name

 

@Id

@Id annotation is used to mark the current field/variable as a primary key  in the table

 

@Table

@Table annotation is also used instead of @Entity.@Table is used to change the details of tables

 

@Column

@Column is used to change the name and length of a column in the table

 

@Transient

The field annotated with transient annotation  is not created/mapped as a column in the table

 

@Temporal

@Temporal annotation is used to specify the format of data in the table like if we are using “@Temporal(TemporalType.TIMESTAMP)“ then we can store both date and time in the field and if we are using @Temporal(TemporalType.DATE) then we can store only date into that field and if we are using @Temporal(TemporalType.TIME) then we can store only time into that field 

 

@Lob 

This annotation specifies that the property should be stored/saved as a large object in the database.

 

@JoinTable

This annotation is used to change the details of the join table.

 

@OneToOne

This annotation is used to perform one-to-one mapping between 2 tables

 

@OneToMany

This annotation is used to perform one-to-many mapping between 2 tables

 

@ManyToMany

This annotation is used to perform many-to-many mapping between 2 tables

 


For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

Annotations in Springboot

@Controller

@Controller annotation is used to mark a class as a controller that has request mapping handlers that handle requests coming from an html or JSP pages of the same application

 

@RequestMapping

It is a method-level annotation that specifies that the current method is mapped as request mapping handlers which handle requests coming from HTML or JSP pages of the same application

 

@RestController

@RestController annotation is used to mark a class as a controller that has request mapping handlers that handle requests coming from another application

 

@RequestParam

It is used to bind/add request parameters with method parameters

 

@ModelAttribute

It is used to  bind/add request parameters with an Object

 

@Service

It is class-level annotation. This class consists of methods called as services used to perform specific operations. Basically these methods are service providers

 

@PathVariable

It is used to access different kinds of parameters from request

 

@GetMapping

This is a method-level annotation used for mapping HTTP requests onto specific handler methods. And that handler method is used to fetch data from one application to another

 

@PostMapping

This is a method-level annotation used for mapping HTTP post request onto specific handler methods. And that handler method is used to add/post  data from one application to another

 

@PutMapping

This is a method-level annotation used for mapping HTTP put requests onto specific handler methods. And that handler method is used to update  data from one application to another

 

@DeleteMapping

This is a method-level annotation used for mapping HTTP delete requests onto specific handler methods. And that handler method is used to delete  data from one application to another

 

@SpringBootApplication:

this annotation Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

 

@RequestBody

Annotation is used to transfer an object from one application to another by mapping data into the body of the request object.  The data present inside the request body is sent by the client-side controller

 

@ResponseBody

When we used  @ResponseBody on the request mapping handler method then spring converted the return value and writes it to the http response object

 

 @EnableAutoConfiguration 

We can enable the auto-configuration feature by using the annotation @EnableAutoConfiguration. Actually we dont need to use this annotation because by default @SpringbootApplication annotation is already applied on the main() method and this @SpringbootApplication annotation contains @EnableAutoConfiguration  internally

 

Do watch the video on Java: Click Here 

 

Author:-

Pooja Nandode-Bhavsar
Call the Trainer and Book your free demo class for Java now!!!

© Copyright 2020 | SevenMentor Pvt Ltd

Submit Comment

Your email address will not be published. Required fields are marked *

*
*