【问题标题】:Getting Null pointer exception while Injecct the dependent Object using autowired annotaion [duplicate]使用自动装配注释注入依赖对象时出现空指针异常[重复]
【发布时间】:2018-08-20 19:30:01
【问题描述】:

你好,我是春天的新手,有人可以帮我解决这个问题吗?

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

1)员工豆

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

}

}

=======================================主类======= ======================

 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>

当我运行这个程序时,我得到的地址对象总是 Null

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

姓名:Shubham Goswami

ID:12201

姓名:9760063335

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

【问题讨论】:

  • 尝试从Employee 中删除Address getter 和setter。在设置null 时可能存在冲突。

标签: java spring spring-annotations


【解决方案1】:

XmlBeanFactory 不注册 AutowiredAnnotationBeanPostProcessor,并且 &lt;context:annotation-config /&gt; 在这种情况下被忽略。使用任何 ApplicationContext 代替 BeanFactory。或者您可以手动注册所需的后处理器。

【讨论】:

  • 是的,它适用于应用程序上下文
猜你喜欢
  • 2011-12-26
  • 1970-01-01
  • 2013-04-10
  • 2014-11-19
  • 2012-02-21
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多