Java 使用@Autowired setter进行自动布线,给出NullPointerException

Java 使用@Autowired setter进行自动布线,给出NullPointerException,java,spring,annotations,autowired,Java,Spring,Annotations,Autowired,春天我是个新手。我开始学习使用@Autowired setter方法进行自动布线。我创建了一个小应用程序并尝试运行,但出现空指针异常: com.develop.Tiger.toString(Tiger.java:29)上线程“main”java.lang.NullPointerException中的异常 在com.develope.Test.main(Test.java:16) 我实现的应用程序如下 Color.java package com.develop; public class Col

春天我是个新手。我开始学习使用@Autowired setter方法进行自动布线。我创建了一个小应用程序并尝试运行,但出现空指针异常:

com.develop.Tiger.toString(Tiger.java:29)上线程“main”java.lang.NullPointerException中的异常 在com.develope.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." ;

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

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

}
Tiger.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." ;

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

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

}
Test.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." ;

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

    }
}
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> 

在中更改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());
    }

}
这是输出

老虎有白色的底色和灰色的纹理颜色


为什么
applicationcontext.xml
中的包名与实际类
(Tiger,Color)
不同?applicationcontext.xml中的限定类名“com.cfdevshop.Color”和“com.cfdevshop.Tiger”与包含java类Color和Tiger的包“com.develop”不一致。