NullpointerException bean中的Dao

时间:2011-04-28 15:38:19

标签: java spring nullpointerexception

在使用GWT,Hibernate和Spring开发的Web应用程序中,我在application-context.xml文件中设置jobClass bean时会遇到。

我在运行时得到一个NullPointerException,absenceDao为null:

这是我的Java类:


public class ExampleJob extends QuartzJobBean {
    private AbsenceDao absenceDao; 
    @Override
    protected void executeInternal(JobExecutionContext context)
    throws JobExecutionException {
        List untreatedDemands = new ArrayList();
        untreatedDemands = absenceDao.getDemandsAskedNotValidated();
    }
    public AbsenceDao getAbsenceDao() {
        return absenceDao;
    }
    public void setAbsenceDao(AbsenceDao absenceDao) {
        this.absenceDao = absenceDao;
    }
}

这是我的application-context.xml:

classpath:internal.properties

<!-- Configuration du crontrigger -->
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <ref local="exampleJob1" />
    </property>

    <property name="cronExpression">
        <util:constant static-field="fr.web.utils.APP_VAR.CRON_EXPRESSION" />
    </property>
</bean>

<bean id="jobClass" class="fr.web.utils.ExampleJob">
    <property name="absenceDao" ref="absenceDao"/>
</bean>

<bean id="exampleJob1" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="fr.web.utils.ExampleJob" />
    <property name="jobDataAsMap">
        <map>
            <entry key="timeout" value="5" />
        </map>
    </property>
</bean>

<!-- Bean containing all the properties of the application -->
<bean class="fr.web.utils.ApplicationProperties" id="applicationProperties" lazy-init="true" scope="singleton">
    <constructor-arg index="0" value="classpath:internal.properties"/>
</bean>

<!-- Bean DAO -->
<bean abstract="true" id="abstractDao">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="fr.web.dao.AbsenceDao" id="absenceDao" parent="abstractDao"/>

</bean>

<!-- Configuration du crontrigger --> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref local="exampleJob1" /> </property> <property name="cronExpression"> <util:constant static-field="fr.web.utils.APP_VAR.CRON_EXPRESSION" /> </property> </bean> <bean id="jobClass" class="fr.web.utils.ExampleJob"> <property name="absenceDao" ref="absenceDao"/> </bean> <bean id="exampleJob1" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="fr.web.utils.ExampleJob" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="5" /> </map> </property> </bean> <!-- Bean containing all the properties of the application --> <bean class="fr.web.utils.ApplicationProperties" id="applicationProperties" lazy-init="true" scope="singleton"> <constructor-arg index="0" value="classpath:internal.properties"/> </bean> <!-- Bean DAO --> <bean abstract="true" id="abstractDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean class="fr.web.dao.AbsenceDao" id="absenceDao" parent="abstractDao"/> </bean>

我认为在jobClass bean中设置一个引用Dao的属性就足够了,但很明显,我错了。

编辑:如果我在exampleJob1 bean中设置Dao属性,我在运行时会收到此错误:


1 个答案:

答案 0 :(得分:2)

不,你必须在exampleJob1 bean配置中设置DAO属性来引用DAO bean。

有必要在app config中使用bean定义,但还不够。您还必须告诉Spring bean工厂如何将它们连接在一起。

您可以将@Autowired注释添加到Java类。只要你打开了注释,这就可以解决它。