行定义XML无效

时间:2014-05-15 08:50:30

标签: java xml spring nullpointerexception annotations

Spring在调用@PostCOnstruct方法时抛出一个NPE。显然,它不会初始化仅在XML文件中的bean内部指定的一个变量。

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="blueprint" class="com.rfslabs.TheDAO">

        <property name="SLEEP_TIME" value="1"/>
        <property name="LIFESPAN_BIG" value="60"/>
        <property name="LIFESPAN_SMALL" value="20"/>
        <property name="ALERT_SIZE" value="80"/>
        <property name="MAX_SIZE" value="3072"/>
        <property name="BUILD_FOLDER" value="/var/www/clients/client2/web6/web/builds/"/>
        <property name="EXISTING" value="4"/>

        <property name="ALERT_L1" value="none"/>
        <property name="ALERT_L2" value="none"/> 

    </bean>

</beans>

在此之后为null的变量:

ApplicationContext con = new FileSystemXmlApplicationContext("res/config.xml");

dao = (TheDAO) con.getBean("blueprint");

是字符串

ALERT_L2 

我为TheDAO类中的所有变量提供了setter和getter,请亲自看看:

    public class TheDAO {

public int SLEEP_TIME;//SECONDS
public int LIFESPAN_BIG;//SECONDS
public int LIFESPAN_SMALL;//SECONDS
public int ALERT_SIZE;//IN %
public int MAX_SIZE;//IN MB
public int EXISTING;
public File BUILD_FOLDER;
public String ALERT_L1;
public String ALERT_L2;

public String getALERT_L1() {
    return ALERT_L1;
}
public void setALERT_L1(String aLERT_L1) {
    ALERT_L1 = aLERT_L1;
}

public String getALERT_L2() {
    return ALERT_L2;
}
public void setALERT_L2(String aLERT_L2) {
    ALERT_L1 = aLERT_L2;
}

public int getEXISTING() {
    return EXISTING;
}
public void setEXISTING(int eXISTING) {
    EXISTING = eXISTING;
}

    public int getSLEEP_TIME() {
    return SLEEP_TIME;
}
public void setSLEEP_TIME(int sLEEP_TIME) {
    SLEEP_TIME = sLEEP_TIME;
}

public int getLIFESPAN_BIG() {
    return LIFESPAN_BIG;
}
public void setLIFESPAN_BIG(int lIFESPAN_BIG) {
    LIFESPAN_BIG = lIFESPAN_BIG;
}

public int getLIFESPAN_SMALL() {
    return LIFESPAN_SMALL;
}
public void setLIFESPAN_SMALL(int lIFESPAN_SMALL) {
    LIFESPAN_SMALL = lIFESPAN_SMALL;
}

public int getALERT_SIZE() {
    return ALERT_SIZE;
}
public void setALERT_SIZE(int aLERT_SIZE) {
    ALERT_SIZE = aLERT_SIZE;
}

public int getMAX_SIZE() {
    return MAX_SIZE;
}
public void setMAX_SIZE(int mAX_SIZE) {
    MAX_SIZE = mAX_SIZE;
}

public File getBUILD_FOLDER() {
    return BUILD_FOLDER;
}
public void setBUILD_FOLDER(File bUILD_FOLDER) {
    BUILD_FOLDER = bUILD_FOLDER;
}

@PostConstruct
public void init(){

    ALERT_SIZE = ( ALERT_SIZE * MAX_SIZE ) / 100;    

    SLEEP_TIME *= 1000;

    LIFESPAN_BIG *= 1000;
    LIFESPAN_SMALL *= 1000;

    if(!ALERT_L1.equals("none")){
    ALERT_L1 = "sudo php " + ALERT_L1;
    }

    if(!ALERT_L2.equals("none")){
        ALERT_L2 = "sudo php " + ALERT_L2;
        }

}


}

到目前为止,程序崩溃了:

if(!ALERT_L2.equals("none")){
        ALERT_L2 = "sudo php " + ALERT_L2;
        }
}

...因为ALERT_L2 null 。我认为这是春天的错。

2 个答案:

答案 0 :(得分:3)

public void setALERT_L2(String aLERT_L2) {
    ALERT_L1 = aLERT_L2;
}

您没有设置ALERT_L2而是设置ALERT_L1。纠正它,它应该工作。

答案 1 :(得分:1)

setter中的值赋值不正确

public void setALERT_L2(String aLERT_L2) {
    //ALERT_L1 = aLERT_L2;
    this.ALERT_L2 = aLERT_L2; //should be
}