Could not autowire. No beans of ‘ILoginService’ type found.
iLoginService显示错误,错误信息:Could not autowire. No beans of ‘ILoginService’ type found.
@Controller
@RequestMapping("user")
public class TestController {
@Autowired
private ILoginService iLoginService;
@PostMapping("login")
@ResponseBody
public LoginResponse test(@RequestParam("username")String userName, @RequestParam("password")String pwd) {
return iLoginService.loginResponse(userName, pwd);
}
}
主要原因没有吧所有对象交给Spring管理,类比springmvc中通过applicationContext.xml注入整个所有包的对象:
<context:component-scan base-package="com.mysutdy.springservlet">
</context:component-scan>
springboot也应该在:DemoApplication中注入整个包对象,可以用:
@ComponentScan("com.example.demo")
但是由于已经有了:
```java
@SpringBootApplication
注解,而且该注解里边包含注入包对象的东西,base-package,所以最后形式是:
@SpringBootApplication(scanBasePackages = "com.example.demo")
//@EnableAutoConfiguration(exclude ={ DataSourceAutoConfiguration.class})//排除自动配置
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
如此一开,就把所有包注入进去,但是要主意应该在 controller service model层加上@controller @service @Repository注解这样代表他们吧自己的对象都发放到spring的IOC中,让它进行管理与分配。