Java spring bean配置错误

时间:2011-04-28 12:54:41

标签: java spring dependency-injection javabeans

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

我在运行时收到此错误:

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]:
Initialization of bean failed;

nested exception is org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [fr.web.utils.ExampleJob] to required type [java.lang.Class] for property 'jobClass';

nested exception is java.lang.IllegalArgumentException:
Cannot convert value of type [fr.web.utils.ExampleJob] to required type [java.lang.Class] for property 'jobClass':
PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value

这是我的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:

    <!-- variables d'environnement - fichier properties -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="placeholderconfig">
        <property name="fileEncoding" value="UTF-8"/>
        <property name="locations">
            <list>
                <value>classpath:internal.properties</value>
            </list>
        </property>
    </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" ref="jobClass" />
        <property name="jobDataAsMap">
            <map>
                <entry key="timeout" value="5" />
            </map>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- 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>
</beans>

2 个答案:

答案 0 :(得分:2)

属性jobClass需要class并且您已经提供了reference bean,所以更改

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

到(注意value以下属性)

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

答案 1 :(得分:0)

这里有另一种选择你可能认为更整洁,我喜欢它,因为它保持上下文更清洁。

<bean id="exampleJob1" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass">
    <bean factory-bean="jobClass" factory-method="getClass" />
  </property>
</bean>

问题的崩溃正如Jigar Joshi描述的解决方案略有不同。 它允许您保持bean jobClass实例化与JobDetailbean的构建分开。