如何在春天使用autowire概念?

时间:2015-06-30 09:29:54

标签: java spring spring-mvc autowired

我是Spring Framework的新手。我正在努力研究Autowired概念,但我的输出不正确。我使用了波纹管代码。我不知道我哪里错了。任何人都可以帮我解决这个问题吗?

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;



}

输出: 我的国家是:null 我的名字是无聊的

4 个答案:

答案 0 :(得分:1)

您可以在bean的属性名称之前使用@Autowired注释 -

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

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

如果你像这样使用autowire,那么你不必使用getter和setter方法。

答案 1 :(得分:1)

首先,使用大写字母启动类名是一个很好的java练习,例如,您的Employee类应该以{{1​​}}而不是E开头。

<强> Employee.java

e

其次,为什么要在填充 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); } } 类实例变量时创建多个bean。像下面一样创建单个bean,并将Employeename设置为country类的属性。

<强>的applicationContext.xml

Employee

接下来,在您的 <bean id="b1" class="com.autowire.Employee"> <property name="name" value= "A"/> <property name="country" value= "India"/> </bean> 课程中,避免不必要的导入并调用main bean,如下所示:

<强> Main.java

b1

答案 2 :(得分:0)

Razib说的很好,但是还不必在你要注入的类中添加注释(@ Component,@ service等)?

答案 3 :(得分:0)

首先需要在Employee类中添加check作为属性。 所以你应该这样做,

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);

然后打印员工对象,

这应该给出预期的结果。