在我使用 GWT、Hibernate 和 Spring 开发的 Web 应用程序中,我在 application-context.xml 文件中设置 jobClass bean 时遇到。
我在运行时收到 NullPointerException,带有 absenceDao
为空:
这是我的 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
<pre><code><!-- 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>
</code></pre>
我认为在 jobClass bean 中设置一个引用 Dao 的属性就足够了,但显然,我错了。
编辑:如果我在 exampleJob1 bean 中设置 Dao 属性,我会在运行时收到此错误:
Error 500 Error creating bean with name 'schedulerFactory' defined in class path resource [application-context.xml]:
Cannot resolve reference to bean 'cronTrigger' while setting bean property 'triggers' with key [0];
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'cronTrigger' defined in class path resource [application-context.xml]:
Cannot resolve reference to bean 'exampleJob1' while setting bean property 'jobDetail';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'exampleJob1' defined in class path resource [application-context.xml]:
Error setting property values;
nested exception is org.springframework.beans.NotWritablePropertyException:
Invalid property 'absenceDao' of bean class [org.springframework.scheduling.quartz.JobDetailBean]:
Bean property 'absenceDao' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?
最佳答案
不可以,您必须在 exampleJob1
bean 配置中设置 DAO 属性才能引用 DAO bean。
应用程序配置中必须有 bean 定义,但这还不够。您还必须告诉 Spring bean 工厂如何将它们连接在一起。
您可以将@Autowired
注释添加到Java类中。只要您打开注释,就可以解决这个问题。
关于java - bean 中的 NullpointerException Dao,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821226/