Java NoSuchBeanDefinitionException:未定义名为“三角形”的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24850252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoSuchBeanDefinitionException: No bean named 'triangle' is defined
提问by Vishal K Chadha
I am new to spring. I am getting
我是春天的新手。我正进入(状态
NoSuchBeanDefinitionException: No bean named 'triangle' is defined
NoSuchBeanDefinitionException:未定义名为“三角形”的 bean
with a simple java app.
用一个简单的java应用程序。
I am getting the following Error Msg
我收到以下错误消息
Jul 20, 2014 7:44:44 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@383118: startup date [Sun Jul 20 07:44:44 EDT 2014]; root of context hierarchy
Jul 20, 2014 7:44:44 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@47eaec: defining beans []; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'Triangle' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at com.springDemo.main.DrawingApp.main(DrawingApp.java:22)
my contextBean.xml
我的 contextBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="triangle" class="com.springDemo.main.Triangle1" >
<property name="type" value="Equilateral" />
</bean >
</beans>
DrawingApp.java
绘图应用程序
package com.springDemo.main;
import org.springframework.core.io.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
private static ApplicationContext context;
static String[] springConfig =
{
"classpath::*/com/springDemo/main/contextBean.xml"
};
public static void main(String[] args)
{
context = new ClassPathXmlApplicationContext(springConfig);
Triangle triangle =( Triangle )context.getBean("triangle");
triangle.draw();
}
}
回答by Jens
there are two :
in the classpath definition. try to remove on:
:
类路径定义中有两个。尝试删除:
"classpath:*/com/springDemo/main/contextBean.xml"
回答by Reimeus
It's likely that contextBean.xml
is not being loaded by Spring. Try loading the context file directly, i.e. without wildcard and the aforementioned :
character
很可能contextBean.xml
没有被 Spring 加载。尝试直接加载上下文文件,即没有通配符和上述:
字符
context =
new ClassPathXmlApplicationContext("/com/springDemo/main/contextBean.xml" );
回答by Braj
You are already looking in class pathusing ClassPathXmlApplicationContext
hence there is no need to add classpath::*
.
您已经在使用 类路径查找,ClassPathXmlApplicationContext
因此无需添加classpath::*
.
Simply removeit and see your problem will be gone.
只需将其删除,您的问题就会消失。
ctx = new ClassPathXmlApplicationContext("com/springDemo/main/contextBean.xml");
You can try with FileSystemXmlApplicationContext
as well where you have to use classpath:
您也可以FileSystemXmlApplicationContext
在必须使用的地方尝试classpath:
ctx = new FileSystemXmlApplicationContext("classpath:com/springDemo/main/contextBean.xml");