Java Spring AutoWire byName

时间:2016-06-15 12:51:54

标签: java spring nullpointerexception autowired

我目前正在做关于Spring的新手教程,似乎我和制作本教程的人一样,但我一直都会遇到错误NullPointerException,有人可以帮帮我吗?

如果你取消注释,我的xml中有一个部分被注释,程序将起作用。但是没有它,当我尝试使用autowire byName时它不起作用。

主类:

public class MainSpring {

    public static void main(String[] args) {

    ApplicationContext context= new ClassPathXmlApplicationContext("konfiguracja.xml");
    SomeBean bean = context.getBean("SomeBean",SomeBean.class);

    System.out.println(bean.getInjectedBean1().getName());
    System.out.println(bean.getInjectedBean2().getName()); 
    }
}

InjectedBean Class

package springTutorial;

public class InjectedBean {

    private String name;

    public String getName() {
        return name;
    }

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

}

SomeBean Class

import org.springframework.beans.factory.annotation.Autowired;

public class SomeBean {

    private InjectedBean InjectedBean1;
    private InjectedBean InjectedBean2;
    public InjectedBean getInjectedBean1() {
        return InjectedBean1;
    }
    public void setInjectedBean1(InjectedBean injectedBean1) {
        this.InjectedBean1 = injectedBean1;
    }
    public InjectedBean getInjectedBean2() {
        return InjectedBean2;
    }
    public void setInjectedBean2(InjectedBean injectedBean2) {
        this.InjectedBean2 = injectedBean2;
    }



}

Configuration.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:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">


    <bean id="InjectedBean1" class="springTutorial.InjectedBean" >

    <property name="name" value="asasa" >


    </property>
    </bean>

    <bean id="InjectedBean2" class="springTutorial.InjectedBean" >

    <property name="name" value="vbvb">

    </property>
    </bean>

    <bean  id="SomeBean" class="springTutorial.SomeBean" autowire="byName" >

<!--    <property name="injectedBean1"> -->

<!--    <ref bean="InjectedBean1"/> -->

<!--    </property> -->


<!--    <property name="injectedBean2"> -->

<!--    <ref bean="InjectedBean2"/> -->
<!--    </property> -->

    </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

根据Configuration.xml; InjectedBean1,InjectedBean2不会注入任何值。

所以基本上InjectedBean1和InjectedBean2都是null。

在null上,您正在尝试获取操作,这导致空指针异常。

为InjectedBean1和InjectedBean2添加ref将解决问题。

<bean id="SomeBean" class="com.stackoverflow.SomeBean" autowire="byName">
        <property name="injectedBean1">
            <ref bean="inject1" />
        </property>
        <property name="injectedBean2">
            <ref bean="inject2" />
        </property>
    </bean>