当我试图将我的版本库启动从我的Application类转移到config类时,我得到了一个错误。这看起来很奇怪,因为urlRepository.save调用没有抛出,而urlRepository.findAll()却得到一个null。我到底做错了什么?
@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));
}
}
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;
}
....
MongoUrlRepository应该是一个接口,这就是Spring可以自动管理它的crud实现的方式。由于你把它声明为一个类,你基本上重载了内置的finder方法,只是返回null,这就是你的NPE的来源。
请看spring.io.的例子。https:/spring.ioguidesgsaccessing-data-mongodb。
为什么在第1种情况下可以工作。
@Autowired
private UrlRepository urlRepository = new MongoUrlRepository();
你实例化了一个MongoUrlRepository,然而在依赖注入过程中,该对象将被替换,由于你没有指定限定词,spring将构建一个托管的MongoRepository Bean(基于你的UrlRepository接口)并忽略你的MongoUrlRepository类。