Here is my XML configuration file. 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.
<?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. 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. The application context is null in both cases of ImportResource
and manual loading of XML after the application started. It worked when it is loaded in CommandLineRunner
's run()
method. I think until the completion of CommandLineRunner
implementations, the application context is not prepared completely.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.