首页 > 其他分享 >springboot在test的时候,new的类报空指针

springboot在test的时候,new的类报空指针

时间:2023-02-15 13:57:19浏览次数:32  
标签:fifthGithubCrawler springboot expoJson 报空 test new FifthGithubCrawler public

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

相关文章

  • SpringBoot 参数及性能优化
    SpringBoot性能优化 组件自动扫描带来的问题默认情况下,我们会使用@SpringBootApplication注解来自动获取应用的配置信息,但这样也会给应用带来一些副作用。使用这个注解后......
  • CentOS中使用Dockerfile部署带websocket的SpringBoot的jar包
    场景CentOS7中使用Dockerfile部署后台jar包在上面使用Dockerfile定制的镜像部署了一个普通的jar包, 如果是jar包里面包含websocket的使用,流程也是一样。websocket所使用的......
  • Java:SpringBoot整合Knife4j(Swagger)提供接口文档
    spring-boot版本2.7.71、引入Maven坐标pom.xml<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId>......
  • SpringBoot常用注解大全
    常用注解概览这里整理了一张SpringBoot常用注解的思维导图,本文主要讲解这些注解的用法。 组件相关注解@ComponentScan默认扫描当前包及其子包下面被@component,@Cont......
  • 基于JAVA+SpringBoot+VUE的心理健康测试系统的设计与实现
    ✌全网粉丝20W+,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战......
  • SpringBoot注解大全
    一、项目配置注解@SpringBootApplication:申明让springboot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和@ComponentScan 三个......
  • 云小课|使用SpringBoot快速构建FunctionGraph HTTP函数
    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说)、深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云。更多精彩内容请单击......
  • springboot基础1
    springboot@RestController//为了返回字符串@RequestMapping用来处理请求地址映射@RestController//为了返回字符串publicclassHelloController{//接口:http......
  • springBoot集成flowable
    前言Flowable可以十分灵活地加入你的应用/服务/构架。可以将JAR形式发布的Flowable库加入应用或服务,来嵌入引擎。以JAR形式发布使Flowable可以轻易加入任何Java环境:Java......
  • 新必应(New Bing)国内申请必修课
     1、下载安装Edgedev版本,这个版本可以直接申请,主要是这个版本成功几率比较大,毕竟它自己的浏览器   下载地址:https://www.microsoftedgeinsider.com/zh-cn/downl......