Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Auto detect Components in Spring.

    • 0
    • 2
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 474
    Comment on it

    When we define the annotations @Component, @Service, @Repository, @Controller with classes, Spring automatically scan and identifies those classes and register the Bean definition with the ApplicationContext.

    The @Component annotations are:

    @Controlller - a web controller ( Indicates a controller component in the presentation laye).

    @Service - a business service facade ( Indicates a Service component in the business layer).

    @Repository - a data access object (Indicates DAO component in the persistence layer).

    @Autowired- to autowire the bean.

    Example

    In the below example we can show the Bean Autowiring using the @Autowired Annotation and it can auto detect the Department Bean and wire them to the Employee Bean. There is no need to use the ref=dept in the bean tag of Department. We can easily get the reference of Department and print the name of Department Name.

    Employee.java

    package com.babita;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class Employee {
    
        private int id;
        private String name;
    
        public Department getDepartment() {
            return department;
        }
        public void setDepartment(Department department) {
            this.department = department;
        }
        @Autowired
        private Department department;
    
        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;
        }
    
    
    }
    

    Department.java

    package com.babita;
    
    public class Department {
    
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

    Now define the below code in app-context.xml

    app-context.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.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
       <bean id="dep" class="com.babita.Department">
           <property name="name" value="java"></property>
       </bean>
    
       <bean id="emp" class="com.babita.Employee">
               <property name="name" value="babita"></property>
               <property name="id" value="001"></property>
       </bean>
    </beans>
    

    Now define the below class to run the code:

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.babita.Department;
    import com.babita.Employee;
    import com.manish.Course;
    import com.manish.Student;
    
    public class App {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
            Employee emp=(Employee)ctx.getBean("emp");
            Department dep=emp.getDepartment();
            System.out.println("Department Name:="+dep.getName());
        }
    }
    

    Output:

    Department Name:=java

    Example of @Service and @Repository:

    DAO layer

    Define the DAO class as below:

    EmployeeDAO.java

    package com.example.employee.dao;
    
    @Repository 
    public class EmployeeDAO 
    {
        @Override
        public String toString() {
            return "Hello , We are in EmployeeDAO";
        }   
    }
    

    Service layer

    Define Service class as below:

    EmployeeService.java

    package com.example.employee.services;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import com.example.employee.dao.EmployeeDAO;
    
    @Service
    public class EmployeeService 
    {
        @Autowired
        EmployeeDAO employeeDAO;
    
        @Override
        public String toString() {
            return "EmployeeService [employeeDAO=" + employeeDAO + "]";
        }
    
    }
    

    Now define the below code in app-context.xml

    <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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:component-scan base-package="com.example"/>
    
    </beans>
    

    Now define the below class to run the code:

    App.java

    package com.example.common;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.example.employee.services.EmployeeService;
    
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext context = 
            new ClassPathXmlApplicationContext(new String[] {"app-context.xml"});
    
            EmployeeService emp = (EmployeeService)context.getBean("employeeService");
            System.out.println(emp);
    
        }
    }
    

    Output

    EmployeeService [employeeDAO=Hello , This is EmployeeDAO]
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: