多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);
    }

}

 

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务