2015-06-30 70 views
1

我是Spring框架的新手。我正在努力研究Autowired概念,但我的輸出結果不正確。我使用了波紋管代碼。我不知道我錯在哪裏。任何人都可以幫我解決這個問題嗎?如何在春季使用autowire概念?

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> 

檢查ing.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; 



} 

輸出: 海我的國家是:空海 我的名字ISNULL

+1

對於這種一般性問題,請參考到Spring手冊http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s11.html –

回答

1

首先,用大寫字母開始類名稱是一個很好的練習,例如您的Employee類應該以E而不是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); 
     } 
    } 

其次,爲什麼當你想填充Employee類的實例變量創建多個豆。創建如下的單個bean,並將namecountry設置爲Employee類的屬性。

applicationcontext。XML

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

接下來,在你的main類避免不必要的進口,並調用b1綠豆象下面這樣:

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(); 
    } 
} 
1

你可以像這樣的Bean的屬性名前使用@Autowired註解 -

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

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

如果你使用這樣的autowire那麼你不必使用getter和setter方法。

0

Razib說的很好,但是不需要爲要注入的類添加註釋(@Component,@service等)?

0

您首先需要將檢查添加爲Employee類中的屬性。 所以你應該做這樣的,

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

,然後打印員工對象,

這應該給預期的結果。