我刚开始使用Spring。在一个简单的Java应用程序中,我遇到了以下错误:
NoSuchBeanDefinitionException:未定义名为'triangle'的bean
请帮我解决这个问题。
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)
我的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();
}
}