2017-05-23 98 views
1

我有兩個類Student和Address分別實現IStudent和IAddress接口。學生班與地址班有關係。這就是爲什麼我已經宣佈了它的參考成員。地址值顯示爲null

public class Student implements IStudent { 

    private String code; 
    private String name; 

    @Autowired 
    private IAddress address; 

    @Override 
    public String getCode() { 
     return this.code; 
    } 

    @Override 
    public String getName() { 
     return this.name; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

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


    public IAddress getAddress() { 
     return this.address; 
    } 

    public void setAddress(Address address) { 
     this.address = address; 
    } 


} 

,我有地址類

public class Address implements IAddress{ 

    private String city; 
    private String pinCode; 
    private String houseNo; 
    private String roadName; 
    @Override 
    public String getCity() { 
     return this.city; 
    } 

    @Override 
    public String getPinCode() { 
     return this.pinCode; 
    } 

    @Override 
    public String getHouseNo() { 
     return this.houseNo; 
    } 

    @Override 
    public String getRoadName() { 
     return this.roadName; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    public void setPinCode(String pinCode) { 
     this.pinCode = pinCode; 
    } 

    public void setHouseNo(String houseNo) { 
     this.houseNo = houseNo; 
    } 

    public void setRoadName(String roadName) { 
     this.roadName = roadName; 
    } 


} 

在我applicationContext.xml文件我寫了下面的bean定義

<?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-4.0.xsd"> 
    <bean id="studentbean" class="main.Student"> 
     <property name="code" value="S001"></property> 
     <property name="name" value="Subhabrata Mondal"></property> 

    </bean> 

    <bean id="addressbean" class="main.Address"> 
     <property name="houseNo" value="119/2"></property> 
     <property name="roadName" value="South Avenue"></property> 
     <property name="city" value="Delhi"></property> 
     <property name="pinCode" value="110005"></property> 
    </bean> 

</beans> 

時,我有豆的初始化之後檢查Student對象,名稱和代碼已用setter方法分配。但地址未分配。因此它顯示地址的空值。我使用@Autowired註釋標記了地址。你能幫忙嗎?

ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     Student student = (Student) factory.getBean("studentbean"); 
     System.out.println(student.getAddress()); 

回答

1

你需要明確絲地址不IAddress因爲CI只知道地址,如果你想有線

@Autowired 
    private Address address; 

,或者您需要定義與類型IAddress一個bean,但你一定要做沒有超過實現或春天會變得困惑,如果你有多個實現使用可以限定符來清除模糊

-1

這整個例子有點奇怪,但你可以擺脫@Autowired註釋並使用下面的bean配置inste廣告;

<bean id="studentbean" class="main.Student"> 
     <property name="code" value="S001"></property> 
     <property name="name" value="Subhabrata Mondal"></property> 
     <property name="address" > 
      <ref local="addressbean"/> 
     </property> 
</bean>