【问题标题】:spring framework null pointer exceptionspring框架空指针异常
【发布时间】:2016-10-02 20:15:26
【问题描述】:

尝试使用属性自动装配 Spring bean,但仍然获得 NPE。片段:

INFO: Loading XML bean definitions from class path resource [autoWireByName.xml]
Exception in thread "main" today do push-ups for 30 mins
java.lang.NullPointerException
    at com.springAutoWireByName.KabadiCoach.getFortune(KabadiCoach.java:23)
    at com.springAutoWireByName.AutoWireByName.main(AutoWireByName.java:13)

KabadiCoach.java

package com.springAutoWireByName;

public class KabadiCoach {

    private SadFortune sadFortune;

    /*public KabadiCoach(){

        System.out.println("inside default Constructor");

    }*/
    public String getDailyWorkout()
    {
        return "today do push-ups for 30 mins";
    }
    public void setSadFortune(SadFortune fortune) {

        sadFortune = fortune;
    }

    public String  getFortune() {       

        return sadFortune.getSadFortune();
    }
}

SadFortune.java

package com.springAutoWireByName;

public class SadFortune {

    public  String getSadFortune()
    {
        System.out.println();
        return "your day wont be good enough Sorry!!!";
    }       
 }
<?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.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

<!-- bean definitions here -->

<bean name="Fortune" class="com.springAutoWireByName.SadFortune">
</bean>
<bean id="myCoach" class="com.springAutoWireByName.KabadiCoach" autowire="byName" />


<!-- this is just a prototype to define actual just make use of this file-->

</beans>

主要

package com.springAutoWireByName;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutoWireByName {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("autoWireByName.xml");

        KabadiCoach co = context.getBean("myCoach",KabadiCoach.class);
        System.out.println(co.getDailyWorkout());
        System.out.println(co.getFortune());
    }
}

运行上述代码后,当我将方法更改为静态时,我收到了列出的错误消息

public static  String getSadFortune()
{
    System.out.println();
    return "your day wont be good enough Sorry!!!";
}

在第 2 课中,我得到了所需的输出。为什么?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    基本上,SadFortune 成员仍然为空,这就是为什么将 getSadFortune() 方法设为静态时它可以工作的原因。

    到目前为止,您只告诉 Spring 实例化一个 Fortune bean 和一个 KabadiCoach,但您必须告诉 Spring KabadiCoach 中的一些成员也需要自动装配。

    在你的 Spring 配置文件中试试这个:

    <bean id="fortune" class="com.springAutoWireByName.SadFortune"></bean>
    <bean id="myCoach" class="com.springAutoWireByName.KabadiCoach" autowire="byName">
        <property name="fortune" ref="fortune" />
    </bean>
    

    编辑:对不起,过度阅读 autowire="byName" 属性。在这种情况下,您可能只是将名称写成小写。

    <bean id="fortune" class="com.springAutoWireByName.SadFortune"/>
    <bean id="myCoach" class="com.springAutoWireByName.KabadiCoach" autowire="byName"/>
    

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2018-10-01
      • 2019-02-06
      相关资源
      最近更新 更多