22.@component annotation

@component annotation
·         Now, enable spring auto component scanning features.
·         Annotate with @Component to indicate this is class is an auto scan component.
·         Put this “context:component” in bean configuration file, it means, enable auto scanning feature in Spring.
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>

·         The base-package is indicate where your components stored are, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.
·         By default, spring will creates the competent object with starting lower letter. Ex : StudentService to “studentService”. While getBean(“studentService”) we have to give like this
·         To create a custom name for component, you can put custom name like this :@component("ob")
Example
StudentDAO.java
package dao;

import org.springframework.stereotype.Component;

@Component    -----(2)
public class StudentDAO {
        
  public void getData(){    
         System.out.println("Hello, Satya Kaveti");
  }
      
}

StudentService.java
package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import dao.StudentDAO;

@Component-----(2)
public class StudentService {
      
        @Autowired
        StudentDAO studentDAO;   
        
        public void getData(){
               studentDAO.getData();           
        }

       public StudentDAO getStudentDAO() {
              return studentDAO;
       }     
}

Spring.xml’
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd">

       <!-- 1. Activate Annotations in XML -->
       <context:annotation-config />
       <context:component-scan base-package="dao" />
       <context:component-scan base-package="service" /> -----(1)

</beans>
StudentMain.java
package main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.StudentService;

public class StudentMain {
       public static void main(String[] args) {       
              ApplicationContext context=new ClassPathXmlApplicationContext("res/spring.xml");
              StudentService ob  = (StudentService)context.getBean("studentService"); -----(3)
              ob.getData();
       }
}

Output
log4j:WARN Please initialize the log4j system properly.
Hello, Satya Kaveti

(1) By Looking this Spring Container understands that in this package it should perform auto-scan Operation
(2) By Looking this, it’s a Component and aucto-scans the properties and create the Object with Staring lower letter
(3) By this we can get the Object
If You want to customize the Object name just put @Component(“Ob”)
We can Retrive StudentService ob  = (StudentService)context.getBean("Ob");

·         You will noticed that all @Repository,@Service or @Controller are annotated with @Component.
·         So, can we use just @Component for all the components for auto scanning?
·         Yes, you can, and spring will auto scan all your components with @Component annotated.
·         It’s working fine, but not a good practice, for readability,
·         you should always declare @Repository,@Service or @Controller for a specified layer to make your code more easier to read

Post a Comment

Thank You

Previous Post Next Post