java – SpringBoot错误:没有名为’myController’的bean可用
我正在
SpringBoot中构建一个“hello world”的基本程序
码 MyController.java package controllers; import org.springframework.stereotype.Controller; @Controller public class MyController { public String hello() { System.out.println("Hello World"); return "foo"; } } DemoApplication.java package di.prac; import java.util.Arrays; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import controllers.MyController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { ApplicationContext ctx=SpringApplication.run(DemoApplication.class,args); MyController m = (MyController)ctx.getBean("myController"); m.hello(); System.out.println("*******"+Arrays.asList(ctx.getBeanDefinitionNames())); } } 我正在使用eclipse并从http://start.spring.io/创建了这个项目而没有任何依赖. 我了解到Spring使用名称myController创建了MyController类的bean,但Spring无法找到myController bean 错误
请查找并解释项目中的错误 解决方法
将控制器置于di.prac的子包下,如di.prac.controllers,或在控制器上使用@ComponentScan
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |