错误码:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-04 10:44:15.629 ERROR 9471 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field testservice in org.minp.vhrsstest.HelloController required a bean of type 'org.minp.vhr.service.test.Testservice' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.minp.vhr.service.test.Testservice' in your configuration.
Process finished with exit code 1
看错误信息的意思是spring找不到这个bean,也就是扫描不到
处理方式分两种
- 保证springboot主启动类要在所有bean的同级或者上级packge
- 通过 scanBasePackge=“org.minp” 指定扫描package
举例
项目分为三个模块,mapper,service,web,分别由maven管理
dao
package org.minp.vhr.mapper;
public interface UserMapper {
void addUser(User user);
}
service
package org.minp.vhr.service;
@Service
public class Helloservice {
@Autowired
UserMapper usermapper;
public void helloService(){
System.out.println("helloService");
}
public void addUserService(User user) {
usermapper.addUser(user)
}
}
web
package org.minp.vhr.start;
@MapperScan(basePackages = "org.minp.vhr.mapper")
@SpringBootApplication(scanBasePackages = "org.minp")
public class VhrssTestApplication {
public static void main(String[] args) {
SpringApplication.run(VhrssTestApplication.class, args);
}
}
@RestController
public class HelloController {
@Autowired
Helloservice helloservice;
@GetMapping("/hello")
public String hello() {
helloservice.helloService();
System.out.println("hello Controller");
return "hello";
}
@PostMapping("/adduser")
public void adduser(){
User user = new User();
user.setId = "1";
user.setName = "minp";
helloservice.addUserService(user);
System.out.println("add user success");
}
}
scanBasePackge 可添加多个package进行扫描
scanBasePackge={“org.minp.vhr1”,“org.minp.vhr2”,“org.minp.vhr3”}