基本Bean配置疑问

发布于 2024-12-05 10:51:22 字数 6701 浏览 2 评论 0原文

我正在关注这个链接: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2024-12-12 10:51:22

例外是关于您的 loader bean。在您的 bean 工厂或应用程序上下文中找不到它:

<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>

jobDataAsMap 属性有一个条目试图引用另一个名为 rRun 的 bean,而该 bean 在您的 bean 中不存在配置。您可能打算将您的类 Runner 添加到您的 bean 配置中,如下所示:

<bean id="rRun" class="package.of.your.class.Runner" />

这将允许 loader bean 在其应用程序上下文中找到您的类。

The exception is in regard to your loader bean. It cannot be found in your bean factory or application context:

<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>

The jobDataAsMap property has an entry that is attempting to reference another bean named rRun, which doesn't exist in your bean configuration. You probably mean to be adding your class Runner into your bean configuration, like so:

<bean id="rRun" class="package.of.your.class.Runner" />

This will allow the loader bean to find your class in its application context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文