0
@Component
public class TempTry implements CommandLineRunner{

@Autowired
TokenRepository tkeRepo;

@Parameter(names = { "--email", "-e" })
String email;

static final Logger logger = LoggerFactory.getLogger(TempTry.class);

@Override
public void run(String... args) throws Exception {
    logger.info("ApplicationStartupRunner run method Started !!");
    TempTry main = new TempTry();
    JCommander.newBuilder().addObject(main).build().parse(args);
    main.runtask();
}

public void runtask() {

    LocalDateTime expiryTime = LocalDateTime.now().plusDays(1);
    String uuid = UUID.randomUUID().toString();
    TokenEntity tknEntity = new TokenEntity();

    tknEntity.setEmailId(email);
    tknEntity.setExpiryTime(DateUtils.asDate(expiryTime));
    tknEntity.setStatus(ResetPasswordStatus.ACTIVE);
    tknEntity.setToken(uuid);

    tkeRepo.save(tknEntity);

    String fromString = UUID.nameUUIDFromBytes("SomeString".getBytes()).toString();
    System.out.println("For email " + mail + " UUID=" + uuid + " is stored at time " + new Date());
    System.out.println("UUID generated from String is " + fromString);
}
}

I set run configuration as -e dhanrajtijare@gmail.com ..getting email value as expected. My problem here is at line tkeRepo.save(tknEntity); tkeRepo is null

Dhanraj
  • 1,162
  • 4
  • 18
  • 45
  • `tkeRepo` has nothing to do with command line args. It should be autowired. To find out why it's not wired, we'd need to see how you're configuring your `TokenRepository` bean, and how you're initialising the Spring container. I suggest you **edit** the question so that it has a https://stackoverflow.com/help/mcve (what you have so far is not *complete* but please also keep it *minimal*) – slim Apr 26 '19 at 11:06
  • How to configure tokenRepository...why it's getting null? – Dhanraj Apr 26 '19 at 11:10
  • Possible duplicate of [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – slim Apr 26 '19 at 11:11
  • Either you are creating instance of bean yourself or you are running it impropertly. – Antoniossss Apr 26 '19 at 11:23

1 Answers1

3

Here

 TempTry main = new TempTry();

How do you expect @Autowire to work if you are creating instance yoursefl?

Your current instance seems to be in app context by that point, so

@Override
public void run(String... args) throws Exception {
    logger.info("ApplicationStartupRunner run method Started !!");

    JCommander.newBuilder().addObject(this).build().parse(args);
    runtask();
}

I put aside the fact that IMHO its just bad to mix JCommander with Spring CLI - use either one or another.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • @Antoniossss Can you tell me how to get command Line property email in my example using spring CLI. – Dhanraj Apr 26 '19 at 11:56
  • Check examples repo as well as Spring Shell docs https://github.com/spring-projects/spring-shell/tree/master/spring-shell-samples/src/main/java/org/springframework/shell/samples – Antoniossss Apr 26 '19 at 12:03