[英]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. 当它在CommandLineRunner
的run()
方法中加载时,它可以工作。 I think until the completion of CommandLineRunner
implementations, the application context is not prepared completely. 我认为在CommandLineRunner
实现完成之前,应用程序上下文还没有完全准备好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.