这是我的XML配置文件。当我尝试加载它时,它给出了一个错误。但是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
我尝试在服务器启动后通过ImportResource注释,CommandlineRunner接口和手动调用来导入它。这是Application类文件。
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);
}
}
谢谢。
答案 0 :(得分:0)
Joeri Hendrickx的评论给了我一个解决方案。在ImportResource
和应用程序启动后手动加载XML的两种情况下,应用程序上下文都为null。它在CommandLineRunner
run()
方法中加载时有效。我想在完成CommandLineRunner
实现之前,应用程序上下文并没有完全准备好。