多model的情况下Consider defining a bean of type错误排查

今天遇到一个问题,就是在多Model的SpringBoot项目里,明明已经加了@Service注解,但是在启动的时候还是无法启动,报出下面的错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-13 18:23:51.180 ERROR 1546 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field studentService in com.demo.web.controller.StudentController required a bean of type 'com.demo.service.StudentService' that could not be found.


Action:

Consider defining a bean of type 'com.demo.service.StudentService' in your configuration.


Process finished with exit code 1

首先先排查一个问题,自己的jar有没有导全

web model下面的pom.xml

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dao</artifactId>
        </dependency>

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>service</artifactId>
        </dependency>

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>entity</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

service model下面的pom.xml

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>dao</artifactId>
        </dependency>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>entity</artifactId>
        </dependency>

dao model 下面的pom.xml

        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>entity</artifactId>
        </dependency>

         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

第二个就是有没有将报错的类加入到容器中。

dao model

//或者在启动类上面加一个MapperScan("mapper包的全路径")
@Mapper 
public interface StudentMapper {

    @Select("SELECT * FROM students WHERE id=#{id}")
    Student findStudentById(int id);
}

或者是这种情况

@SpringBootApplication
@MapperScan(basePackages = "com.demo.dao")
public class StartApplication {

    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }

}

 service model

public interface StudentService {

    Student findStudentById(int id);
}
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public Student findStudentById(int id) {

        return studentMapper.findStudentById(id);

    }
}

controller Model

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/find_by_id")
    public Student getStudent(@Param("id") int id){
        Student student= studentService.findStudentById(id);
        return student;
    }
}

当你在确保这@Mapper、@Service、@RestController三个注解都已经被加上了之后还是报出这样的错误之后,你可以再去看下面这种情况。

 是不是在你的启动类,并没有将其他Model给扫进去。如果不去特别声明的话就只会扫当前的Model。

web model下面的启动类:

@SpringBootApplication(scanBasePackages = "com.demo")
@MapperScan(basePackages = "com.demo.dao")
public class StartApplication {

    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }

}

 

全部评论

相关推荐

05-23 19:02
吉林大学 Java
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 13:05
TMD找工作本来就烦,这东西什么素质啊😡
Beeee0927:hr是超雄了,不过也是有道理的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务