今天做Spring quartz定时任务时,出现一些问题,特此记录一下,以免下次再次出现,供大家和自己参考:
1、org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.test.db.BeanFactoryPost]
for bean with name 'xx.xxxxx.xxx' defined in ServletContext resource
原因是caused by java.lang.ClassNotFoundException(类找不到,未编译)。但发现类的路径并未写错,后来通过各种找bug,才发现是因为类没有编译,WEB-INF/下根本没有classes文件。
解决办法:工程->build path->default output path->更改为:工程名/WebRoot/WEB-INF/classes(原来是:工程名/build/classes),spring不会去build/classes路径下找。更改完之后,clean 工程即可编译。
2、如何注入另外一个工程的class?(Class<?> jobClass属性)
<property name="jobClass" value="org.test.job.TestJob2"/></bean>
或:
xmlns:p="http://www.springframework.org/schema/p" ........ p:jobClass="org.test.job.TestJob2"
而发现,通过事先定义一个org.test.job.TestJob2的bean,再ref是会报异常的:
即:
<property name="jobClass" ><ref bean="job2" /></property></bean>
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [org.test.job.TestJob2] to required type [java.lang.Class] for property 'jobClass': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.test.job.TestJob2]
3、org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dsSys' defined in ServletContext resource [/WEB-INF/conf/spring/sys/context.xml]: Could not resolve placeholder 'dsSys.jdbc.url' in string value "${dsSys.jdbc.url}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx.jdbc.url' in string value "${xxx.jdbc.url}"
出现这个异常时,无非是找不到相应的配置文件。但我检查了很久,发现配置文件的路径并没有写错!后来发现——我在不同的xml文件读取了不同的.properties文件,即:
aa.xml
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/conf/sys/aaa.properties</value> </property> </bean>
bb.xml
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/conf/sys/bb.properties</value> </property> </bean>
由此造成了冲突,需要把读取多个配置文件写在一起:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/conf/sys/aa.properties</value> <value>/WEB-INF/conf/sys/bb.properties</value> </list> </property> </bean>