使用@Autowired setter自动装配NullPointerException

时间:2014-12-10 11:02:15

标签: java spring annotations autowired

我是春天的新人。我开始使用@Autowired setter方法学习自动装配。我创建了一个小应用程序并尝试运行并获得空指针异常:

线程中的异常" main" com.develop.Tiger.toString的java.lang.NullPointerException(Tiger.java:29) 在com.develop.Test.main(Test.java:16)

我按照以下方式实施应用

Color.java

package com.develop;
public class Color {    
    private String baseColor;   
    private String textureColor;

    public String getBaseColor() {
        return baseColor;
    }
    public void setBaseColor(String baseColor) {
        this.baseColor = baseColor;
    }
    public String getTextureColor() {
        return textureColor;
    }
    public void setTextureColor(String textureColor) {
        this.textureColor = textureColor;
    }
    @Override
    public String toString() {
        return baseColor + " base skin color and " + textureColor + " texture color." ;

    }
}

Tiger.java

package com.develop;
import org.springframework.beans.factory.annotation.Autowired;
public class Tiger {

    private String name;
    private Color colorBean;

    public String getName() {
        return name;
    }

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

    public Color getColorBean() {
        return colorBean;
    }

    @Autowired
    public void setColorBean(Color color) {
        this.colorBean= color;
    }

    @Override
    public String toString() {
        return "The " + name + " has " + colorBean.toString();

    }
}

Test.java

package com.develop;    
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

    public static void main(String[] args) {
        Resource resource=new ClassPathResource("applicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory(resource);         

        //AutoWire By @annotation
        Tiger tiger = (Tiger) factory.getBean("tigerBean");
        System.out.println(tiger.toString());
    }

}

的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" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">  

     <context:annotation-config />

    <bean id="colorBean" class="com.develop.Color">
        <property name="baseColor" value="white" />
        <property name="textureColor" value="grey" />
    </bean>


      <!-- Auto-Wiring with @Autowired annotation -->
      <bean id="tigerBean" class="com.develop.Tiger">
        <property name="name" value="tiger" />
     </bean>

</beans> 

1 个答案:

答案 0 :(得分:1)

中更改Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  

        //AutoWire By @annotation
        Tiger tiger = (Tiger) context.getBean("tigerBean");
        System.out.println(tiger.toString());
    }

}

这是输出

  

老虎有白色基础肤色和灰色纹理颜色。