【问题标题】:SpringBoot error : No bean named 'myController' availableSpring Boot 错误:没有名为“我的控制器”的 bean 可用
【发布时间】:2018-10-02 01:17:48
【问题描述】:

我正在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 创建了名为 myControllerMyController 类的 bean,但是 Spring 找不到 myController bean

错误

线程“main”中的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 名为“myController”的 bean 可在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:686) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) 在 di.prac.DemoApplication.main(DemoApplication.java:16)

请找出项目中的错误并解释

【问题讨论】:

  • 您的控制器与您的演示应用程序类位于不同的包中。
  • MyController 放入di.prac。这将允许 spring boot 执行自动组件扫描

标签: java spring spring-boot


【解决方案1】:

将您的控制器放在di.prac 的子包下,如di.prac.controllers 或在您的控制器上使用@ComponentScan。默认情况下,Spring 会扫描您的主应用程序所在的当前包和子包。如果你也想扫描其他包,那么你可以在@SpringBootApplication 中指定包作为参数。

@SpringBootApplication(scanBasePackages = {"com.xyz.controllers", "com.abc.models""})

我们应该避免将@Configuration 类放在默认包中(即根本不指定包)。在这种情况下,Spring 会扫描类路径中所有 jar 中的所有类。这会导致错误并且应用程序可能无法启动。

【讨论】:

    【解决方案2】:

    为了让您的控制器在 Spring 上下文中可用,您需要定义它由 Spring 容器管理。只有@Controller 注解是不够的,它只表示你的bean 的原型,还有注解@Repository 和@Service。

    如果 bean 具有这些注释并由 Spring 管理,这是因为 Spring 正在扫描以搜索它们的包已经以编程方式或每个 xml 指定。在您的情况下,您应该使用其他 2 个注释来注释您的 DemoApplication 类:

    1. @Configuration - 允许访问 spring 上下文
    2. @ComponentScan - Spring 要扫描的包

      @Configuration
      @ComponentScan (basePackages = {"controllers"})
      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())); 
          }
      }
      

    【讨论】:

      【解决方案3】:

      刚刚遇到同样的问题,解决方法很简单。你只是(我也)在错误的地方创建了包“控制器”。它不应该在 java 文件夹中创建,而是在具有项目名称的文件夹下创建。简单但致命的错误。你的代码写得很好。

      【讨论】:

        猜你喜欢
        • 2020-12-02
        • 2021-07-08
        • 2018-10-14
        • 2021-03-18
        • 2017-12-16
        • 2018-02-21
        • 2019-10-20
        • 2020-05-24
        • 1970-01-01
        相关资源
        最近更新 更多