BeanInitializationException:bean需要属性

时间:2014-05-13 19:28:41

标签: spring spring-batch job-scheduling spring-scheduled

我得到了:

BeanInitializationException: Property 'job' is required for bean 'jobScheduler'

即使我有一个正确定义的“batch:job”bean,我也有一个指向它的属性声明,如下所示:

<batch:job id="userSummary">
    ...
</batch:job>

<bean id="jobScheduler" class="cc.glance.server.business.fact.JobScheduler">
    <property name="job" ref="userSummary" />
</bean>

注意:“userSummary”的定义应该没有问题,因为它之前工作正常,当我通过Web控制器调用它而不是调度时。

堆栈追踪:

2014-05-14 11:54:08,022 | ERROR | ion(5)-127.0.0.1 | o.a.c.c.C.[.[.[/]                | apache.juli.logging.DirectJDKLog  185 | Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobScheduler' defined in URL [jar:file:/C:/VICTOR/TRABALHO/glance/glance-core/server/glance-server-webapp/target/glance-server-webapp-2.2.2-SNAPSHOT/WEB-INF/lib/glance-server-business-2.2.2-SNAPSHOT.jar!/cc/glance/server/business/fact/JobScheduler.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'job' is required for bean 'jobScheduler'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973) [catalina.jar:7.0.53]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467) [catalina.jar:7.0.53]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[catalina.jar:7.0.53]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) ~[catalina.jar:7.0.53]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) ~[catalina.jar:7.0.53]
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632) ~[catalina.jar:7.0.53]
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1740) ~[catalina.jar:7.0.53]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) ~[tomcat-coyote.jar:7.0.53]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[na:1.7.0_25]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[na:1.7.0_25]
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618) ~[catalina.jar:7.0.53]
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565) ~[catalina.jar:7.0.53]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) ~[tomcat-coyote.jar:7.0.53]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[na:1.7.0_25]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[na:1.7.0_25]
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487) ~[na:1.7.0_25]
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97) ~[na:1.7.0_25]
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328) ~[na:1.7.0_25]
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420) ~[na:1.7.0_25]
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848) ~[na:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) ~[na:1.7.0_25]
    at sun.rmi.transport.Transport$1.run(Transport.java:177) ~[na:1.7.0_25]
    at sun.rmi.transport.Transport$1.run(Transport.java:174) ~[na:1.7.0_25]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_25]
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173) ~[na:1.7.0_25]
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553) ~[na:1.7.0_25]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808) ~[na:1.7.0_25]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667) ~[na:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_25]
    at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'job' is required for bean 'jobScheduler'
    at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 54 common frames omitted

JobScheduler类:

@Component
public class JobScheduler
{
    @Autowired
    private JobLauncher jobLauncher;

    private Job job;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Required
    public void setJob(Job job)
    {
        this.job = job;
    }

    public void run()
    {
        try
        {
            Date endDate = new Date();
            Date beginDate = DateUtils.addWeeks(endDate, -1);
            String summaryId = beginDate.toString(); //TODO proper summaryId

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addDate("dateFrom", beginDate);
            builder.addDate("dateTo", endDate);
            builder.addString("summaryId", summaryId);

            JobParameters param = builder.toJobParameters();

            JobExecution execution = jobLauncher.run(job, param);
            logger.info("Exited UserSummary job scheduling with status: " + execution.getStatus());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

正如@AndreiStefan建议的那样,问题在于如何加载spring XML文件。

我没有加载我在这里复制的XML(使用&#34; userSummary&#34; job和&#34; jobScheduler&#34; bean)。它只是从另一个XML引用,将其设置为&#34; AutomaticJobRegistrar&#34;它扫描它的工作(只有工作,没有其他豆)。这就是我在网页控制器中工作的原因,正如我在问题中所说的那样。现在&#34; jobScheduler&#34;我定义的bean没有工作,所以它没有按照我想要的属性集加载。

现在我已将JobScheduler类注释为@Component。这就是上下文仍然加载它的原因,尽管没有必需的属性,从而引发了异常。

为解决这个问题,我移动了&#34; jobScheduler&#34;定义到上下文正在加载的另一个XML(它是在applicationContext.xml导入的文件夹上)。

答案 1 :(得分:0)

回答与@Vituel类似的问题。为了更具体一点,我既加载了XML文件中定义的bean,又指定为@Component。即使我正在适当地定义属性,它们也会按需要出现,而不是指定:

<bean id="newMappingFileController" class="...NewMappingFileController">
    <property name="sasMappingBO" ref="sasMappingBO" />
</bean>
...
Error...
Caused by: ...BeanInitializationException: Property 'sasMappingBO' is required...

对我来说,解决方案只是从类中删除@Component注释,并且工作正常。