首页 > 解决方案 > 通过 GenericApplicationContext 注册 SpringBoot bean 不起作用

问题描述

我在运行时这里有一个简单的 bean 注册,但它不起作用:

@GetMapping("/hi")
public void hi()
{
    MyClass myClass = new MyClass();
    genericApplicationContext.registerBean("mybean", MyClass.class, myClass);
}

@GetMapping("/bye")
public Integer bye()
{
    MyClass myClass = (MyClass) genericApplicationContext.getBean("mybean");
    return myClass.hi;
}

调用方法hi()一切正常,但是当我调用bye()方法时,我得到了这个异常:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybean': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)] with root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybean': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

这里发生了什么?

标签: javaspringspring-bootjavabeansspring-bean

解决方案


推荐阅读