java - 获取 NoSuchBeanDefinitionException
问题描述
线程“主”org.springframework.beans.factory.NoSuchBeanDefinitionException 中的异常:org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805) 在 org.springframework.beans 中没有名为“greetingControler”的 bean .factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:202) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) 在 vill.example.village.VillageApplication.main(VillageApplication.java:15)
产生此异常的类是
package controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import services.GreetingService;
@Controller
public class GreetingControler {
private GreetingService greetingService;
@Autowired
public void setGreetingService(GreetingService greetingService) {
this.greetingService = greetingService;
}
public String sayHello()
{
String greeting=greetingService.getGreeting();
System.out.println(greeting);
return greeting;
}
}
解决方案
Replace the method setGreetingService(GreetingService greetingService) with class constructor.
@Autowired
public GreetingControler (GreetingService greetingService) {
this.greetingService = greetingService;
}
推荐阅读
- azure-ad-b2c - 自定义电子邮件验证示例似乎无法正常工作
- python-3.x - 如何编写 Mock unittest 来检查 git_status?
- python - 公会成员
- c# - 将 Razor 库资源移动到 MVC 发布文件夹
- node.js - FS readFile 在获取请求中不返回任何内容
- odoo - Odoo 控制器接收 blob
- java - 迭代 ArrayList 并添加到具有特定格式的另一个 arrayList
- azure - 将 Azure Key Vault 与 ASP.NET 4.0 集成
- php - 为 foreach() 和 index.php 提供的无效参数不显示任何内容
- c - 使用c struct实现链表