ok
@Component public class FifthGithubCrawler { @Autowired private KBComponentVersionRepository versionRepository; /** * 导出所有数据到json文件 */ public void expoJson() throws IOException { List<KBComponentVersion> all = versionRepository.findAll(); Gson gson=new GsonBuilder().create(); // FileWriter writer = new FileWriter("/opt/fossx/data/testdownload/staff.json"); 。。。 }
@SpringBootTest(classes = Start.class) public class FifthGithubCrawlerTest { @Resource private FifthGithubCrawler fifthGithubCrawler; @Test public void test() throws IOException { fifthGithubCrawler.expoJson(); } }
如上是正确的
使用fifthGithubCrawler.expoJson();的时候不可以直接不能new FifthGithubCrawler()因为在FifthGithubCrawler中有一个自动注入的KBComponentVersionRepository。
若没有经过spring容器(new xxx),那么自动注入不会生效,这时调用该类的方法就会报空指针错误
标签:fifthGithubCrawler,springboot,expoJson,报空,test,new,FifthGithubCrawler,public From: https://www.cnblogs.com/wYYBLH/p/17122529.html