@Autowired 实体越来越空
@Autowired entity is getting null
@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
这里
TempTry main = new TempTry();
如果您自己创建实例,您希望@Autowire
如何工作?
到那时您的当前实例似乎在应用上下文中,所以
@Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunner run method Started !!");
JCommander.newBuilder().addObject(this).build().parse(args);
runtask();
}
我抛开一个事实,即恕我直言,将 JCommander 与 Spring CLI 混合使用是不好的 - 使用其中一个或另一个。
@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
这里
TempTry main = new TempTry();
如果您自己创建实例,您希望@Autowire
如何工作?
到那时您的当前实例似乎在应用上下文中,所以
@Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunner run method Started !!");
JCommander.newBuilder().addObject(this).build().parse(args);
runtask();
}
我抛开一个事实,即恕我直言,将 JCommander 与 Spring CLI 混合使用是不好的 - 使用其中一个或另一个。