Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • @Resource annotaion in Spring

    • 0
    • 2
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 475
    Comment on it

    @Resource annotation allows you to specify a name of the injected bean. @Resource annotation takes a 'name' attribute which will be treated as the bean name we want to inject in the class. In other words we can say, it follows by-name autowiring ( same as @Autowired but follows by name). If there is no "name" is specified explicitly with annotation, then the name is extracted from the name of the annotated setter method or field, or it is taken from the named-Parameter.

    Ex:-

    Department.java

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

    In the below class we have used @Resource annotation to inject the Department bean.

    Employee.java

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

    Now define the below code in 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;
    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

    We can see in the above example that we have used @Resource(name="dep") annotation to inject the bean of type "com.babita.Department" in the Employee class.

    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: