22.@Component @Repository @Service Complete Example

@Component @Repository @Service Complete Example
·         in real life, at DAO and manager layer we often have separate classes and interfaces.
·          Interface for defining the contract, and classes for defining the implementations of contracts. Where to use these annotations

Always use these annotations over concrete classes; not over interfaces.

·         Working of all 3 are same
·         But Using specific type of stereotype for specific type of Component  is Recommended
@Component Ã  Use in Bo, Bean Classes
@Repository  Ã  Use in DAO classes
@Service        Ã  Use in Service Layer classes

Example
StudentBo.java
package satya.bo;

import org.springframework.stereotype.Component;

@Component("bo")
public class StudentBo {
private int id;
private String name;
private String address;
public int getId() {
       return id;
}
public void setId(int id) {
       this.id = id;
}
public String getName() {
       return name;
}
public void setName(String name) {
       this.name = name;
}
public String getAddress() {
       return address;
}
public void setAddress(String address) {
       this.address = address;
}

public void getData(){
       System.out.println("======STUDENT DATA ===========");
       System.out.println(getId()+"\n"+getName()+"\n"+getAddress());
}


}

StudentDAO.java
package satya.dao;

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

import satya.bo.StudentBo;

@Repository("dao")
public class StudentDAO {
      
       @Autowired
       StudentBo bo;
       public void getData(){
              bo.getData();       
       }
      
}

StudentService.java
package satya.service;

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

import satya.dao.StudentDAO;

@Service("service")
public class StudentService {

       @Autowired
       StudentDAO dao;
       public void getData(){
       dao.getData();      
}
}

StudentMain.java
package satya;

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

import satya.bo.StudentBo;
import satya.service.StudentService;

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

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="satya.bo" />
       <context:component-scan base-package="satya.dao" />
       <context:component-scan base-package="satya.service"></context:component-scan>
      
       <bean id="bo" class="satya.bo.StudentBo">
              <property name="id" value="100"></property>
              <property name="name" value="Satya"></property>
              <property name="address" value="VIJAYAWADA"></property>
      
       </bean>
</beans>

Output
======STUDENT DATA ===========
100
Satya
VIJAYAWADA

Post a Comment

Thank You

Previous Post Next Post