简体   繁体   English

加载批处理XML配置时找不到Spring bean,但在应用程序上下文中可用

[英]Spring beans not found while loading batch XML configuration, but available in application context

Here is my XML configuration file. 这是我的XML配置文件。 When I am trying to load it, it is giving an error.But the bean is already present in the application context, which is used to fetch some data and is working perfectly fine. 当我尝试加载它时,它给出了一个错误,但是bean已存在于应用程序上下文中,该上下文用于获取一些数据并且运行良好。

<?xml version="1.0" encoding="UTF-8"?>
    <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.xsd   http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd   ">
    <description>Batch Configuration</description>
    <property-placeholder
        xmlns="http://www.springframework.org/schema/context"
        location="./analytics-config.properties" />
    <bean class="org.springframework.batch.core.scope.StepScope" />
    <bean
        class="com.intellect.riskanalytics.batch.jobs.instruments.InstrumentsReader"
        id="instrumentReader" scope="step">
        <property name="dataService" ref="dataService" />
        <property name="stepExecution" value="#{stepExecution}" />
    </bean>
    <bean
        class="com.intellect.riskanalytics.batch.jobs.instruments.InstrumentsProcessor"
        id="instrumentProcessor">
        <constructor-arg index="0" ref="dataService" />
    </bean>
    <bean
        class="com.intellect.riskanalytics.batch.jobs.instruments.DurationsWriter"
        id="durationsWriter" scope="step">
        <property name="dataService" ref="dataService" />
        <property name="stepExecution" value="#{stepExecution}" />
    </bean>
    <job xmlns="http://www.springframework.org/schema/batch" id="durationsJob">
        <step id="abstractIsinDurationsStep">
            <tasklet>
                <chunk commit-interval="${batch.writer.chunk-size}"
                    processor="instrumentProcessor" reader="instrumentReader"
                    writer="durationsWriter" />
            </tasklet>
        </step>
    </job>
</beans>


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataService' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1213)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 30 more

I tried importing it through, ImportResource annotation, CommandlineRunner interface, and a manual call after the server has started. 我尝试通过ImportResource批注,CommandlineRunner接口和服务器启动后的手动调用来导入它。 This is the Application class file. 这是应用程序类文件。

import java.util.Arrays;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.intellect.riskanalytics.util.Logger;

/**
 * Startup class for launching the application by Spring Boot
 *
 * @see SpringBootApplication
 * @see EnableBatchProcessing
 * @see EnableCaching
 * @since 1.0
 */
@SpringBootApplication
@EnableBatchProcessing
@EnableCaching
@ImportResource("classpath:analytics-batch-test.xml")
public class AnalyticsApplication {
    private static final Logger logger = new Logger(AnalyticsApplication.class);

    @Autowired
    private static ApplicationContext applicationContext;

    /**
     * Main method to be executed for starting the application in spring context
     *
     * @param args
     *            the command line arguments if any
     */
    public static void main(String[] args) {
        logger.info("Starting Risk Analytics Application With Arguments:" + Arrays.toString(args));
        SpringApplication.run(RiskAnalyticsApplication.class, args);
        logger.info("Started Risk Analytics Application");
        createBatchJobsContext("");
    }

    private static void createBatchJobsContext(String batchXMLLocation) {
        batchXMLLocation = "classpath:riskanalytics-batch-test.xml";
        logger.info("Creating Batch XML Application Context with: " + batchXMLLocation);
        // Tried this
        ApplicationContext batchXMLContext = new ClassPathXmlApplicationContext(new String[] { batchXMLLocation },
                applicationContext);
        // And this
        ApplicationContext batchXMLContext = new ClassPathXmlApplicationContext(batchXMLLocation);

        logger.info("Created Batch application context: " + batchXMLContext);
    }
}

Thank you. 谢谢。

Joeri Hendrickx 's comment gave me a solution. Joeri Hendrickx的评论给了我一个解决方案。 The application context is null in both cases of ImportResource and manual loading of XML after the application started. ImportResource和应用程序启动后手动加载XML的情况下,应用程序上下文均为null。 It worked when it is loaded in CommandLineRunner 's run() method. 当它在CommandLineRunnerrun()方法中加载时,它可以工作。 I think until the completion of CommandLineRunner implementations, the application context is not prepared completely. 我认为在CommandLineRunner实现完成之前,应用程序上下文还没有完全准备好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用上下文中部分bean的依赖在Spring批处理中形成循环 - The dependencies of some of the beans in the application context form a cycle in Spring Batch Spring Application Context配置:找不到类异常 - Spring Application Context configuration: class not found exception 找不到应用程序上下文的Spring配置文件 - application-context Spring configuration file not found 关于在Spring Framework应用程序中使用Beans.xml配置文件 - About the use of Beans.xml configuration file in Spring Framework application 加载xml应用程序上下文ioexception spring - loading xml application context ioexception spring 春季:找不到application-context.xml - Spring: application-context.xml not found 找不到Spring Application Bean异常 - Spring Application Beans Not Found Exception Spring上下文加载两次xml和annotation配置 - Spring context loading twice with both xml and annotation configuration SPRING-BATCH ERROR:在spring批处理应用程序中使用threadPoolExecutor时,没有可用于步骤范围的上下文持有者 - SPRING-BATCH ERROR: No context holder available for step scope when using threadPoolExecutor in spring batch application 当spring-servlet.xml尝试自动装配application-context.xml中定义的bean时发生错误 - Error when spring-servlet.xml tries to autowire beans defined in application-context.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM