使用自动装配注释注入依赖对象时获取空指针异常

时间:2018-03-12 17:16:10

标签: java spring spring-annotations

嗨,我春天很新,有人可以帮我这个。

问题 - 我使用的是Spring 3.0,我需要使用自动线注释,我有2个豆

1)员工bean

2)地址bean

public class Employee {
private String name ;
private Long id;
private String mobileNumber;
@Autowired
private Address address;

public Employee() {
    System.out.println("Default Constructor Employee class");
}

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

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getMobileNumber() {
    return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {
    this.mobileNumber = mobileNumber;
}

public void display() {
    System.out.println("Name : "+getName());
    System.out.println("ID : "+getId());
    System.out.println("Name : "+getMobileNumber());
    if(getAddress()!=null) {
        getAddress().getAddressInfo();
    }

}

public Address getAddress() {
    return address;
}


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

===============================二=============== ==========================

  package com.as.autowire.setterinjection;

  public class Address {

  private String state;
  private String city;
  private String zip;   
  private String street;


public Address() {
    System.out.println("Address Class Default constructor");
}
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getZip() {
    return zip;
}
public void setZip(String zip) {
    this.zip = zip;
}
public void getAddressInfo() {
    System.out.println("Street "+getStreet());
    System.out.println("city "+getCity());
    System.out.println("State "+getState());
    System.out.println("zip "+getZip());

}

}

====================================== main class ======= =====================

 package com.as.autowire.setterinjection;



 import org.springframework.beans.factory.BeanFactory;  
 import org.springframework.beans.factory.xml.XmlBeanFactory;  
 import org.springframework.core.io.*;  

  public class Test {  
  public static void main(String[] args) {  


    BeanFactory factory=new XmlBeanFactory(r);  

    Employee e=(Employee)factory.getBean("emp",Employee.class);  
    e.display();

}  
}  

========================================== 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="emp" class="com.as.autowire.setterinjection.Employee">  
 <property name="name">  <value>Shubham Goswami</value>  </property>  
 <property name="id">  <value>12201</value>  </property>  
 <property name="mobileNumber">  <value>9760063335</value> </property>  
 <!-- <property name="address" ref="address" /> --> 
 </bean>


 <bean id="address" class="com.as.autowire.setterinjection.Address">  
 <property name="Street">  <value>Moh Firahadiyan Jwalapur</value>  </property>  
 <property name="city">  <value>Jwalapur</value>  </property>  
 <property name="state">  <value>Uttarakhand</value> </property> 
 <property name="zip">  <value>249407</value> </property> 

 </bean>

当我运行这个程序时,我的地址对象总是为空

o / p是 -   默认构造函数员工类

姓名:Shubham Goswami

ID:12201

姓名:9760063335

但是在这里我没有得到地址属性,所以可以帮助解决这个问题

1 个答案:

答案 0 :(得分:-1)

XmlBeanFactory不会注册AutowiredAnnotationBeanPostProcessor,在这种情况下会忽略<context:annotation-config />。使用任何ApplicationContext而不是BeanFactory。或者您可以手动注册所需的后处理器。