Java Spring Annotation

Posted By : Avaneesh Kumar Verma | 28-Jun-2018

Some common Annotation used in SpringBoot

Annotations are becoming very common in Java EE Application. It was first introduced in Java 1.5 version. Frameworks like Spring, Hibernate and Web Services etc are using lots of Annotation. Every annotation has their specific functionality which delivered in the code. It removes the difficulties of writing code in XML because it is very difficult and time-consuming to do any task using XML. Annotation provides metadata of the program. It can be parsed by using tools like annotation parsing tool or by the compiler.

We can create custom Annotation, It is like creating an interface but the difference is that we replace the interface keyword by @ Interface.

Sample Program for creating a custom Annotation.

 

package com.example.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Functoinality{
    String createdBy() default "Atul";
    String date();
    int rev() default 1;
    String info();
}

 


Some common Annotation used in Spring are as follows:

1.  @Autowired: It is used to inject the bean dependency in a particular class. It is Autowire ByType internally. Spring Application starts Spring container made the object of all the bean classes stored it in a container and when we use @Autowired annotation the object created by the container is injected in that class where Autowired Annotation is used. If we are autowiring the class other then bean then we will get an error saying "error in creating bean: unsatisfied dependency" or something like that.

2. @Override: This annotation is used when we are overriding super class method. This annotation tells the compiler that the following is overriding super class method and any changes occur in super class method will leads to a compile-time error.

3. @Controller: This annotation used in Spring MVC framework. This annotation tells Spring container that the particular class acts as Controller in the application. The dispatcher scan that class and find the RequestMapping Annotation to map the Request.

4. @Service: This annotation used in Spring MVC framework. This annotation tells the compiler that the particular class is service class. In general, service class contains the logic and communicate with Dao class.

 

About Author

Author Image
Avaneesh Kumar Verma

Avaneesh is a Software developer Having Knowledge Java , J2EE ,Data Structure and Algorithm ,SQL.

Request for Proposal

Name is required

Comment is required

Sending message..