【发布时间】:2019-04-26 19:27:48
【问题描述】:
@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);
}
}
我将运行配置设置为 -e dhanrajtijare@gmail.com ..按预期获取电子邮件值。 我的问题在 tkeRepo.save(tknEntity); tkeRepo is null
行【问题讨论】:
-
tkeRepo与命令行参数无关。它应该是自动接线的。要找出它未连接的原因,我们需要了解您如何配置TokenRepositorybean,以及如何初始化 Spring 容器。我建议你编辑这个问题,让它有一个stackoverflow.com/help/mcve(你到目前为止还没有完整,但也请保持最小 ) -
如何配置 tokenRepository...为什么它会变空?
-
要么您自己创建 bean 实例,要么您运行不正确。
标签: spring spring-boot jcommander