spring中使用quartz时注入时出现的错误
错误1:
配置文件:
<!-- 任务执行器的线程池 --> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="keepAliveSeconds" value="60" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="50" /> </bean> <!-- 定时任务的工作Bean --> <bean id="hourquartzJob" class="com.tasks.HourTaskServiceImpl"> <property name="taskExecutor" ref="taskExecutor" /> </bean> <!-- 定义生成工作对象的工厂,并为工厂设定目标对象targetObject属性、目标对象的工作方法targetMethod属性 --> <bean id="hourjobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="hourquartzJob" /> <property name="targetMethod"> <value>synchronizeDb</value> </property> <property name="concurrent" value="false" /> </bean> <!-- 任务调度计时器,进行定时设置。CronTriggerBean能进行非常精确的定时设置 --> <bean id="hourcronQuartz" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="hourjobDetail" ref="hourjobDetail" /> <!-- cron表达式 --> <property name="cronExpression"> <!-- 0 0 */2 * * ? 每两小时、整点触发 --> <!-- 0 0/2 * * * ? 每两分钟 --> <!-- 0/5 * * * * ? 每五秒钟 --> <!-- 0 15 10 * * ? 每天Y分X点触发 --> <value>0 */5 * * * ?</value> </property> </bean> <!-- 调度任务触发器,启动定时任务--> <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="hourcronQuartz" /> </list> </property> </bean>
错误:
2013-10-23 17:29:19,754] [main] DEBUG - Invoking destroy() on bean with name 'taskExecutor'
[2013-10-23 17:29:19,754] [main] INFO - Shutting down ThreadPoolExecutor 'taskExecutor'
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hourcronQuartz' defined in class path resource [springconfig/spring-quartz-jobs.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'hourjobDetail' of bean class [org.springframework.scheduling.quartz.CronTriggerBean]: Bean property 'hourjobDetail' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1303)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1042)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:413)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:735)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at com.main.Controller.main(Controller.java:19)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'hourjobDetail' of bean class [org.springframework.scheduling.quartz.CronTriggerBean]: Bean property 'hourjobDetail' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:805)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:655)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1300)
... 15 more
原因:
<property name="hourjobDetail" ref="hourjobDetail" />
初始化org.springframework.scheduling.quartz.CronTriggerBean时需要注入参数,参数值是setter进行的,参数名也必须匹配。
此处的参数名应为jobDetail
更改如下如下:
<property name="jobDetail" ref="hourjobDetail" />