首页 > 解决方案 > 获取 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) 在 vi​​ll.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;
    }
}

标签: java

解决方案


Replace the method setGreetingService(GreetingService greetingService) with class constructor.

@Autowired
public GreetingControler (GreetingService greetingService) {
    this.greetingService = greetingService;
}

推荐阅读