Can we use @Autowired in a Tasklet in Spring Batch?

OK I found the error.

In your SampleBatch you declare your Bean SampleTasklet in Java Config. That means you have to make sure all dependencies get injected. That is why you do not get the UserService autowired.

Make sure that the UserService gets set in

@Bean
public SampleTasklet sampleTasklet(UserService userService){
    return new SampleTasklet(userService);
}

and do not forget to add this constructor in your SampleTasklet and assign the UserService there.

public class SampleTasklet implements Tasklet {

private UserService userService;

    public SampleTasklet (UserService userService){
        this.userService=userService;
    }

Where is your UserService class located? Since one of the things@SpringBootApplication annotation does is a component scan, but it will only scan on sub-packages. i.e. if your SampleBatchApp class is in com.mypackage, then it will scan for all classes in sub-packages i.e. com.mypackage.*.

Or other alternative is to use a @SpringBootApplication(scanBasePackages = {"com.mypackage"})