NoSuchBeanDefinitionException:没有定义名为“triangle”的bean

时间:2014-07-20 11:58:05

标签: java spring

我是春天新手。我正在

  

NoSuchBeanDefinitionException:没有名为' triangle'已定义

使用简单的Java应用程序。

我收到以下错误消息

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();
    }
}

3 个答案:

答案 0 :(得分:2)

已使用ClassPathXmlApplicationContext查看课程路径,因此无需添加classpath::*

只需删除即可查看您的问题。

ctx = new ClassPathXmlApplicationContext("com/springDemo/main/contextBean.xml");

您也可以尝试FileSystemXmlApplicationContext使用classpath:

ctx = new FileSystemXmlApplicationContext("classpath:com/springDemo/main/contextBean.xml");

答案 1 :(得分:0)

类路径定义中有两个:。尝试删除:

"classpath:*/com/springDemo/main/contextBean.xml"

答案 2 :(得分:-1)

Spring可能没有加载contextBean.xml。尝试直接加载上下文文件,即不使用通配符和前面提到的:字符

context = 
   new ClassPathXmlApplicationContext("/com/springDemo/main/contextBean.xml" );