Spring Framework Annotations

Spring Framework Annotations

Spring framework provides a variety of annotations for a better approach. Using annotations, we can develop Spring applications (small & large scale) easily with less code.

Following are the annotations used in the Spring framework:

@Component – The @Component stereotype annotation is used to indicate a class as “Component” so that Spring can auto-scan the classes for dependency injection. We don’t need to declare the beans using the <bean> tag. It will automatically scan the beans for dependency injection using the <context:component-scan> tag. The @Component annotation is available in the org.springframework.stereotype package.

Following code shows how to use the @Component annotation:

@Component
 public class Jio_Sim implements Sim {
 …..
   } 

@Service – The @Service stereotype annotation is used to indicate a class as “Service” at the class level. Also, it is a specialization of the @Component annotation, which acts as a Business Service Facade. The classes annotated with @Service annotation are auto-detected by the Spring Container. The @Service annotation is available in the org.springframework.stereotype package.

Following code shows how to use the @Service annotation:

@Service
 public class Airtel_Sim implements Sim {
 .......
   } 

@Repository – The @Repository stereotype annotation is used to indicate a class as “Repository” at the class level. Like @Service annotation, the @Repository is also a specialization of the @Component. A class that is used to store, fetch, or search data comes under the Repository category. Usually, DAO classes come under the Repository category for auto-detection. The @Repository annotation is available in the org.springframework.stereotype package.

Following code shows how to use the @Repository annotation:

@Repository
 public class Airtel_Sim implements Sim {
 ....
  } 

@Controller – The @Controller stereotype annotation is used to indicate a class as “Controller” at the class level. It is also a specification of @Component annotation. Therefore, these classes are auto-detected for implementations. It is used in combination with @RequestMapping annotation in Spring MVC. The @Controller annotation is available in the org.springframework.stereotype package.

Following code shows how to use @Controller:

@Controller
 public class Spring_Controller {
 .....
 }  

@Autowired – The @Autowired annotation is used on a constructor, setter or config method, and fields as to be autowired by the Spring dependency injection feature. For autowiring byType, we can use the @Autowired annotation with the field and setter method, and for constructor-based autowiring, we can use the @Autowired with the constructor. The @Autowired annotation is available in the org.springframework.beans.factory.annotation package.

Following code shows how to use @Autowired:

With Constructor

// Defining the Constructor
                 @Autowired
                 public Cricket_Coach(Fortune fortuneservice) {
                 this.fortuneservice = fortuneservice;
   } 

With Setter Method

// Defining the setter method
                 @Autowired
                 public void setFortuneservice(Fortune fortuneservice) {
                 this.fortuneservice = fortuneservice;
   } 

With Field

// Defining the Field
                 @Autowired
                 private Fortune fortuneservice; 

@Qualifier – The @Qualifier annotation is used when there is an autowiring conflict. It is used with a field or parameter as a qualifier for candidate beans during autowiring. The @Qualifier annotation is used along with the @Autowired annotation and available in the org.springframework.beans.factory.annotation package.

Following code shows how to use the @Qualifier annotation:

              @Autowired
                 @Qualifier("goodLuckFortune")
                 public Fortune fortuneservice ; 

@Configuration – The @Configuration annotation indicates a class as “Configuration,” which contains one or more @Bean methods which are processed by the Spring container. It performs all the functionalities of the XML bean-config file and used for dependency injection. The @Configuration annotation is available in the org.springframework.context.annotation package.

Following code shows how to use @Configuration annotation:

@Configuration
 public class Config_App {
 …..
 } 

@Bean – The @Bean is a method-level annotation applied on a method that tells the Spring container to add the annotated method as a bean in the ApplicationContext. It indicates that the Spring container manages the beans produced by a method. The @Bean annotation is available in the org.springframework.context.annotation package.

Following code shows how to use @Bean annotation:

// defining bean for GoodLuckFortune class
                 @Bean
                 public Fortune goodLuckFortune() {
                 return new GoodLuckFortune();
                 } 

@PostConstruct – We use the @PostConstruct annotation on a method that needs to execute after the dependency injection. The method annotated with @PostConstruct must be invoked before the class is put into service. The method on which @PostConstruct is applied may be public, private, or protected.  The @PostConstruct annotation is available in the javax.annotation package.

Following code shows how to use the @PostConstruct annotation:

                @PostConstruct
                 public void doStartUp() {
                 System.out.println( "Inside the init method : doStartUp()" );
                 } 

@PreDestroy – The @PreDestroy annotation is used on a method that needs to be executed just before the removal of beans from the application context. The Spring container is responsible for the removals of the beans. Like @PostConstruct, the method on which @PreDestroy is applied may be public, private, or protected. The @PreDestroy annotation is available in the javax.annotation package.

Following code shows how to use the @PreDestroy annotation:

             @PreDestroy
                 public void doShutDown() {
                 System.out.println( "Inside the destroy method : doShutDown()" );
                 } 

@ComponentScan – The @ComponentScan annotation is used for specifying the package that we want to be scanned by the Spring container. If we use @ComponentScan without argument, it tells the Spring to scan the current package on which we are working. The @ComponentScan annotation is used along with the @Configuration annotation. The @ComponentScan annotation is available in the org.springframework.context.annotation package.

Following code shows how to use the @ComponentScan annotation:

@Configuration
 @ComponentScan( "com.app.SpringBean_usingBean" )
 public class AppConfig {
 ....
  } 

@PropertySource – The @PropertySource annotation provides a mechanism for adding the property files. It is also used in conjunction with @Configuration annotation. According to Java 8, the @PropertySource is repeatable. The @PropertySource annotation is available in the org.springframework.context.annotation package.

Following code shows how to use the @PropertySource annotation:

@Configuration
 @PropertySource("classpath:sport.properties")
 public class AppConfig {
 ....
  } 

@Value – The @Value annotation is used at the field, constructor, or method level, which indicates a value expression for the affected argument.  It is used to insert values into the field or method arguments. The @Value annotation is available in the org.springframework.beans.factory.annotation package.

Following code shows how to use the @Value annotation:

                @Value("${foo.email}")
 private  String email; 

@Scope – The @Scope annotation indicates the name of the scope for the application. It can be used as a type-level or method-level annotation. If we declare the @Scope as a type-level, it must be used in conjunction with @Component or @Configuration annotation else used with @Bean annotation. The @Scope annotation is available in the org.springframework.context.annotation package.

Following code shows how to use the @Scope annotation:

@Component
 @Scope("prototype")
 public class Cricket_Coach implements Coach {
 ....
 }