基本Bean配置疑问
我正在关注这个链接: http://www.mkyong.com/spring/spring-quartz-scheduler-示例/
一切正常。本教程展示了 3 个类和 pom 文件。
我想让它与 2 个课程一起使用。
但是我遇到了问题。
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Loader
{
public static void main( String[] args ) throws Exception
{
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
}
}
这是第二个类
public class Runner extends QuartzJobBean
{
Runner rRun = new Runner();
public void printMe() {
System.out.println(" Quartz Running! ~");
}
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException
{
rRun.printMe();
}
}
这是 pom bean 配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="loader"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="Loader" />
<property name="jobDataAsMap">
<map>
<entry key="rRun" value-ref="rRun" />
</map>
</property>
</bean>
<!--
<bean id="loader"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="rRun" />
<property name="targetMethod" value="printMe" />
</bean>
-->
<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="application.properties"/>
<property name="placeholderPrefix" value="${props:"/>
</bean>
<!-- Simple Trigger -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="loader" />
<property name="repeatInterval" value="${props:repeatInterval}" />
<property name="startDelay" value="${props:startDelay}" />
</bean>
<!-- Cron Trigger -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runner" />
<property name="cronExpression" value="${props:cronExpression}" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="runner" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
</beans>
我哪里出错了?
编辑:
我的问题是:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'runner' defined in class path resource [Spring-Quartz.xml]: Cannot resolve reference to bean 'rRun' while setting bean property 'jobDataAsMap' with key [TypedStringValue: value [rRun], target type [null]]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'rRun' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedMap(BeanDefinitionValueResolver.java:378)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:161)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at Loader.main(Loader.java:9)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'rRun' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 17 more
I am following this link:
http://www.mkyong.com/spring/spring-quartz-scheduler-example/
Everything is working fine. The tutorial shows 3 classes and pom file.
I want to make it work with 2 classes.
However I am having issues.
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Loader
{
public static void main( String[] args ) throws Exception
{
new ClassPathXmlApplicationContext("Spring-Quartz.xml");
}
}
This is the second class
public class Runner extends QuartzJobBean
{
Runner rRun = new Runner();
public void printMe() {
System.out.println(" Quartz Running! ~");
}
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException
{
rRun.printMe();
}
}
This is the pom bean config file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="loader"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="Loader" />
<property name="jobDataAsMap">
<map>
<entry key="rRun" value-ref="rRun" />
</map>
</property>
</bean>
<!--
<bean id="loader"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="rRun" />
<property name="targetMethod" value="printMe" />
</bean>
-->
<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="application.properties"/>
<property name="placeholderPrefix" value="${props:"/>
</bean>
<!-- Simple Trigger -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="loader" />
<property name="repeatInterval" value="${props:repeatInterval}" />
<property name="startDelay" value="${props:startDelay}" />
</bean>
<!-- Cron Trigger -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="runner" />
<property name="cronExpression" value="${props:cronExpression}" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="runner" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
</beans>
Where did I go wrong?
EDIT:
My issues are:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'runner' defined in class path resource [Spring-Quartz.xml]: Cannot resolve reference to bean 'rRun' while setting bean property 'jobDataAsMap' with key [TypedStringValue: value [rRun], target type [null]]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'rRun' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedMap(BeanDefinitionValueResolver.java:378)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:161)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at Loader.main(Loader.java:9)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'rRun' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 17 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例外是关于您的
loader
bean。在您的 bean 工厂或应用程序上下文中找不到它:jobDataAsMap
属性有一个条目试图引用另一个名为rRun
的 bean,而该 bean 在您的 bean 中不存在配置。您可能打算将您的类Runner
添加到您的 bean 配置中,如下所示:这将允许
loader
bean 在其应用程序上下文中找到您的类。The exception is in regard to your
loader
bean. It cannot be found in your bean factory or application context:The
jobDataAsMap
property has an entry that is attempting to reference another bean namedrRun
, which doesn't exist in your bean configuration. You probably mean to be adding your classRunner
into your bean configuration, like so:This will allow the
loader
bean to find your class in its application context.