0

I am trying to autowire one of the configuration class but it is returning null. Can you please help me to figure out what I am doing wrong?

The configuration class looks like below -

package com.xyz.abc.mn.filereader.batch.config;
@Configuration
public class Xyz{

@Value("${property1:#{'09'}}")
private String property1;
...
...
//getters and setters

This is the Util class where I am trying to use the autowire -

package com.xyz.abc.mn.filereader.batch.util;
public class XyzUtil{

@Autowired
private Xyz xyz;

public boolean method1(){
     xyz.getProperty1();  //Giving Null pointer exception as xyz is Null
     ...
     ...

My application class looks like below -

package com.xyz.abc.mn.filereader.batch.config;
@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.xyz.abc.mn.filereader.batch.*""})
@PropertySource(value="file:${props.file}") 

public class MainApplication {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;
...
...
...
Arijit Roy
  • 379
  • 3
  • 14

1 Answers1

1

This is not related to Spring Batch, it is due to a misconfiguration of Spring beans. @Configuration indicates that a class declares one or more @Bean definition methods. This is not the case for your Xyz. You should annotate it with @Component.

Moreover, XyzUtil is not managed by Spring, that's why dependency injection is not performed in that class. You need to annotate it with @Component as well.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50