0

In a spring batch, I'm trying to access a property from application.properties file in my StepExecutionListenerusing spring's @value annotation, but it turns out to be null.

@Component
public class SomeListener implements StepExecutionListener {
 
@Value("${some.property}")
private String foo;

    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println(foo);
    }
 
}

This piece of code print null.

CtrlAltElite
  • 487
  • 8
  • 32
  • 3
    `SomeListener` does not appear to be a Spring Bean, since it doesn't appear to be annotated with `@Component`. How is the object instance created? Injection only works for Spring-managed beans. – Andreas Feb 01 '21 at 03:28
  • 2
    Does this answer your question? [Why is my Spring @Autowired field null?](https://stackoverflow.com/q/19896870/5221149) – Andreas Feb 01 '21 at 03:30
  • @Andreas That was my bad, `SomeListener` is a bean annotated with `@Component`. The issue was that it's object was being created using `new` keyword instead of using the `@Autowired` annotation where it was being used in the Job bean. The link was helpful. If you can post this as an answer, so I can accept. – CtrlAltElite Jul 07 '21 at 21:45

0 Answers0