提问者:小点点

线程“main”org. springframe.bean.工厂中的异常。NoSuchBeanDefitionException:没有名为“大学”的bean可用


我测试的代码


包com. annotations.注解;

导入org. springframe.原型.组件;

@组件("学院")公共课学院{

public void id() {
    System.out.println("33");
}


@配置公共类配置{

@Bean
public College name() {
    return new College();
}

}


import javax.naming.Context;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App 
{
    public static void main( String[] args )
    {
     ApplicationContext mech=new AnnotationConfigApplicationContext(Config.class);
    
     College lokesh=mech.getBean("college",College.class);
     lokesh.id();
     
     
}

我得到这样的错误


线程"main"org. springframe.bean.Factory.NoSuchBeanDefitionException中的异常:org.springframework.beans.factory.support中没有名为"学院"的bean。DefaultListableBeanFactory.getBeanDefition(DefaultListableBeanFactory.java:872)位于org.springframework.beans.factory.support。AbstractBeanFactory.getMergedLocalBeanDefition(AbstractBeanFactory.java:1344)位于org.springframework.beans.factory.support。AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)位于org.springframework.beans.factory.support。AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)位于org.springframework.context.support。AbstractApplicationContext.getBean(AbstractApplicationContext.java:1160)位于com.annotations.annotations.App(App.java:15)



共1个答案

匿名用户

您必须指定bean名称

@Bean(name = "college")
public College name() {
    return new College();
}