码
MyController.java
package controllers;import org.springframework.stereotype.Controller;@Controllerpublic 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;@SpringBootApplicationpublic 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
Exception in thread “main”
@H_419_46@
org.springframework.beans.factory.NoSuchBeanDeFinitionException: No
bean named ‘myController’ available at
org.springframework.beans.factory.support.Defaultlistablebeanfactory.getBeanDeFinition(Defaultlistablebeanfactory.java:686)
at
org.springframework.beans.factory.support.Abstractbeanfactory.getMergedLocalBeanDeFinition(Abstractbeanfactory.java:1210)
at
org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:291)
at
org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:199)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)
at di.prac.DemoApplication.main(DemoApplication.java:16)请查找并解释项目中的错误
解决方法 将控制器置于di.prac的子包下,如di.prac.controllers,或在控制器上使用@ComponentScan 总结以上是内存溢出为你收集整理的java – SpringBoot错误:没有名为’myController’的bean可用全部内容,希望文章能够帮你解决java – SpringBoot错误:没有名为’myController’的bean可用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)