Springboot发现注入service和mapper为null

在一次刚搭建项目结构,想看看自己的dao层是否有效,打算用测试类,测试一下,dao和service有没有用,发现,空指针,后来就试着打印相关mapper和service,发现为空!!!

原来的代码:

@SpringBootTest
public class MybatisTest {
    @Autowired
    DemoService demoService;
    @Autowired
    DemoMapper demoMapper;
    @Test
    public void daoTEST(){
       //demoService.getUser();
        System.out.println(demoMapper);
        System.out.println(demoService);
    }
}

打印结果:
Springboot发现注入service和mapper为null_第1张图片

现在代码:


@RunWith(SpringRunner.class)
@SpringBootTest(classes = SxApplication.class)
public class MybatisTest {
    @Autowired
    DemoService demoService;
    @Autowired
    DemoMapper demoMapper;
    @Test
    public void daoTEST(){
       //demoService.getUser();
        System.out.println(demoMapper);
        System.out.println(demoService);
    }
}

结果 :
Springboot发现注入service和mapper为null_第2张图片

参考链接:https://blog.csdn.net/m0_37830775/article/details/105278710?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163955866516780274185722%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163955866516780274185722&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-1-105278710.pc_search_result_cache&utm_term=springboot%E6%B5%8B%E8%AF%95%E7%B1%BB%E6%B3%A8%E5%85%A5service&spm=1018.2226.3001.4187

你可能感兴趣的:(java中遇到的bug,spring,boot,java)