KVK
KVK

Reputation: 1289

how to work with autowire concept in spring?

I am new to the Spring Framework. I'm trying to work on the Autowired concept, but my output is not correct. I used the bellow code. I don't know where I'm wrong. Can any one help me to fix this?

Employee.java:

package com.autowire;

public class employee {

    private String name;
    private String country;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    public void show()
    {
        System.out.println("hai my country is:"+country);
        System.out.println("hai my name is"+name);

    }

}

main.java:

package com.autowire;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;;


public class main {
    public static void main(String args[]){
        ApplicationContext context=new ClassPathXmlApplicationContext("config/applicationcontext.xml");

        employee emp=(employee) context.getBean("b1");

        emp.show();
    }

}

applicationcontext.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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="b1" class="com.autowire.employee" autowire="byName">

</bean>

<bean id="name" class="com.autowire.checking">
<property name="id" value="12"></property>
<property name="name1" value="yes"></property>
</bean>

<bean id="id" class="com.autowire.checking">
<property name="id" value="12"></property>
<property name="name1" value="yes"></property>
</bean>

</beans>

checking.java

package com.autowire;

public class checking {
    public String getName1() {
        return name1;
    }
    public void setName1(String name1) {
        this.name1 = name1;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    private String name1;
    private int id;



}

output: hai my country is:null hai my name isnull

Upvotes: 1

Views: 180

Answers (4)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

First, it's a good java practice to start class name with Upper Case, for example your Employee class should start with E instead of e.

Employee.java

 package com.autowire;
    public class Employee {

        private String name;
        private String country;

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(final String country) {
            this.country = country;
        }

        public void show() {
            System.out.println("hai my country is: " + country);
            System.out.println("hai my name is: " + name);
        }
    }

Secondly, why create multiple beans when you want to populate Employee class instance variables. Create single bean like below and set name and country as property for Employee class.

applicationcontext.xml

 <bean id="b1" class="com.autowire.Employee">
        <property name="name" value= "A"/>
        <property name="country" value= "India"/>
 </bean>

Next, in your main class avoid unnecessary imports and call the b1 bean like below:

Main.java

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

public class Main {
    public static void main(String args[]) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationcontext.xml");
        Employee emp = (Employee) context.getBean("b1");
        emp.show();
    }
}

Upvotes: 1

Swapnil Gaykhe
Swapnil Gaykhe

Reputation: 301

You first need to add checking as a property in Employee class. so you should be doing like this,

public class Employee {
Checking checking;

public Employee(Checking checking) {
    super();
    this.checking = checking;
}

public Checking getChecking() {
    return checking;
}

public void setChecking(Checking checking) {
    this.checking = checking;
}

@Override
public String toString() {
    return "Employee [checking=" + checking + "]";
}

}

public class Checking {
String name;
String id;

public Checking(String name, String id) {
    super();
    this.name = name;
    this.id = id;
}
@Override
public String toString() {
    return "Checking [name=" + name + ", id=" + id + "]";
}

}

System.out.println(emp);

And then print employee object,

This should give the expected result.

Upvotes: 0

bobK
bobK

Reputation: 754

What Razib said is good, but don't also have to add annotations (@Component, @service, etc) to the class you want to inject?

Upvotes: 0

Razib
Razib

Reputation: 11173

You may use the @Autowired annotation before a property name of a bean like this -

@Autowired
@Qualifier("name")
private String name; 

@Autowired
@Qualifier("country")
private String Country;

If you use autowire like this then you don't have to use the getter and setter methods.

Upvotes: 1

Related Questions