【发布时间】:2020-06-12 11:24:57
【问题描述】:
尝试将存储库启动从我的应用程序类移动到配置类时出现错误。这似乎很奇怪,因为 urlRepository.save 调用没有抛出,而 urlRepository.findAll() 得到一个空值。我在这里做错了什么?
之前(工作正常 - 使用预加载的 URL 打印到控制台)
@SpringBootApplication
public class UrlShortenerApplication implements CommandLineRunner {
@Autowired
private UrlRepository urlRepository = new MongoUrlRepository();
public static void main(String[] args) {
SpringApplication.run(UrlShortenerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String shortlinkStub = "short.li/";
String cascadeDishPods = "https://www.amazon.com/gp/product/B07CTQ8THP/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1";
String flipFlops = "https://www.amazon.com/gp/product/B0013MWDO0/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1";
urlRepository.save(new ShortUrl(shortlinkStub + "flipflops", flipFlops));
urlRepository.findAll().forEach(shortUrl -> System.out.println("Preloaded " + shortUrl));
}
}
之后(repository.findAll() 的空指针异常)
ShortenerConfig.java
@Configuration
public class ShortenerConfig {
public static UrlRepository urlRepository = new MongoUrlRepository();
@Bean("urlRepo")
public UrlRepository urlRepo() {
return urlRepository;
}
}
UrlShortenerApplication.java
@SpringBootApplication
public class UrlShortenerApplication implements CommandLineRunner {
@Autowired
@Qualifier("urlRepo")
private UrlRepository urlRepository;
public static void main(String[] args) {
SpringApplication.run(UrlShortenerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String shortlinkStub = "short.li/";
String cascadeDishPods = "https://www.amazon.com/gp/product/B07CTQ8THP/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1";
String flipFlops = "https://www.amazon.com/gp/product/B0013MWDO0/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1";
urlRepository.save(new ShortUrl(shortlinkStub + "flipflops", flipFlops));
urlRepository.findAll().forEach(shortUrl -> System.out.println("Preloaded " + shortUrl));
}
}
最后一行抛出空指针异常,我不知道为什么!
堆栈跟踪
第 22 行是指“运行”,第 35 行是指 reponsitory.findAll()
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-12 10:48:25.813 ERROR 75530 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at com.example.urlshortener.UrlShortenerApplication.main(UrlShortenerApplication.java:22) ~[main/:na]
Caused by: java.lang.NullPointerException: null
at com.example.urlshortener.UrlShortenerApplication.run(UrlShortenerApplication.java:35) ~[main/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
... 5 common frames omitted
2020-06-12 10:48:25.843 INFO 75530 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
存储库接口和类(适用于两种情况)
public interface UrlRepository extends MongoRepository<ShortUrl, Long> {
public ShortUrl findByShortUrl(String shortUrl);
public List<ShortUrl> findByLongUrl(String longUrl);
}
public class MongoUrlRepository implements UrlRepository {
@Override
public ShortUrl findByShortUrl(String shortUrl) {
return null;
}
@Override
public List<ShortUrl> findByLongUrl(String longUrl) {
return null;
}
....
【问题讨论】:
-
请添加堆栈跟踪。
-
添加了堆栈跟踪
-
findAll是否有可能返回 null? -
它不会在第一个实例中抛出空指针 - 它使用我刚刚保存的“触发器”url打印到控制台。
-
请出示您的 UrlRepository
标签: spring spring-boot javabeans java-11 spring-annotations