What is @Configuration and @Component annotation




@Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context. 




@Configuration is the heart of the Java-based configuration mechanism that was introduced in Spring 3. It provides an alternative to XML-based configuration.


 Here is an example   which will clear the concept of @Configuration.


Import    com.mukesh;
Import    org.springframework.context.annotation.*;

@Configuration

 public  class   SpringConfigExample{

         @Bean
   
        public     MyBean     mybean(){





                   return    new  MyBean();
            }

   }


The above code is same as context file configuration of a bean class mybean.  The method mybean()  return  a object  which consider as bean class . Because of it annotated as @Bean .

  <beans>

   <bean  id=”mybean”   class=”com.mukesh.MyBean”/>

  </beans>





 Java  Example :--




package com.mukesh;
import org.springframework.context.annotation.*;

@Configuration
public class SpringConfigExample {
   @Bean
   public MyBean  mybean(){
      return new  MyBean();
   }
}


Here is the content of  MyBean.java file
package com.mukesh;

public class  MyBean {
   private String message;





   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
  }





Following is the content of the MainApp.java file



package com.mukesh;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx =
         new AnnotationConfigApplicationContext(.class);
  
      MyBean   myBean = ctx.getBean(MyBean.class);
      myBean.setMessage("Hello World!");
      myBean.getMessage();
   }
}




Here   how  you can use properties  file  using  @Configuration


 @Configuration
 @PropertySource("classpath:root/test.props")





public class SampleConfig {

 @Value("${test.prop}")

   private String attr;

  @Bean
 public SampleService sampleService() {
  
    return new SampleService(attr);
 }



 @Bean
 public static PropertySourcesPlaceholderConfigurer     

    placeHolderConfigurer() {
  return new PropertySourcesPlaceholderConfigurer();
 }

}



Here   we  can   use @Configuration   annotation to  import  java class and xml  file . 





import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:/config/spring.xml")
public class AppConfig {





}



AppConfig.java
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Import;





@Configuration
@Import({ AppConfigWeb.class })
@ImportResource("classpath:/config/spring.xml")
public class AppConfig {

}




<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 750*250 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9729080010552617"
     data-ad-slot="1210243387"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>


    Here   we    know  about  @Component   .






Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

We can directly use @Component for each and every bean, but for better understanding and maintainability of a large application, we use @Controller, @Service, @Repository.
Purpose of each annotation:

1) @Controller -> Classes annotated with this, are intended to receive a request from the client side. The first request comes to the Dispatcher Servlet, from where it passes the request to the particular controller using the value of @RequestMapping annotation.

2) @Service -> Classes annotated with this, are intended to manipulate data, that we receive from the client or fetch from the database. All the manipulation with data should be done in this layer.

3) @Repository -> Classes annotated with this, are intended to connect with database. It can also be considered as DAO(Data Access Object) layer. This layer should be restricted to CRUD (create, retrieve, update, delete) operations only. If any manipulation is required, data should be sent be send back to @Service layer.

If we interchange their place(use @Repository in place of @Controller), our application will work fine.
The main purpose of using three different @annotations is to provide better Modularity to the Enterprise application.






What is @Configuration and @Component annotation What   is   @Configuration   and @Component   annotation Reviewed by Mukesh Jha on 9:02 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.