我是Spring的初学者,写了个简单的Spring代码:如下ApplicationContext appCon = new ClassPathXmlApplicationContext( "config.xml" );
WelHello wh = (WelHello) appCon.getBean("hello");
                // 一个输出函数
wh.update("Spring method");我把config.xml文件放在src目录下,这算是把配置文件放在了ClassPath路径下面了吧.
config.xml里的内容为如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
    <bean id="hello" class="zhao.WelHello">
        <property name="strAage" value="Spring" />
    </bean>
</beans>但是我执行这个Main方法,就出现如下错误,顺便问一下,我用的JDK是1.4的,不知道支持Spring不,出现如下错误:2007-12-29 23:46:48 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2007-12-29 23:46:48 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
2007-12-29 23:46:48 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=25377109]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [hello]; root of BeanFactory hierarchy
2007-12-29 23:46:48 org.springframework.context.support.AbstractApplicationContext refresh
信息: 1 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=25377109]
2007-12-29 23:46:48 org.springframework.context.support.AbstractApplicationContext initMessageSource
信息: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@bfea1d]
2007-12-29 23:46:48 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
信息: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@406199]
2007-12-29 23:46:48 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [hello]; root of BeanFactory hierarchy]

解决方案 »

  1.   

    你应该把web.xml里边的配置内容也贴出来。不过有一点。你那个<property   name="strAage"   value="Spring"   />
    你确定没问题吗?你配置了id="Spring" class="***"这个bean?
      

  2.   

    2楼的兄弟,我看资料上就配置了一个config.xml文件,并没看到web.xml配置文件~
    我在IDE上创建工程,自己就加了个config.xml文件,
    我在书上看,例子上写的是,只加了个beans.xml文件,里面内容跟我写的config.xml文件内容一样~
      

  3.   

    web.xml要加配置的,加如下配置:
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
      

  4.   

    但是我执行这个Main方法,就出现如下错误,
    /????????????????
    下面的哪一行信息是错误?你所说的错误是什么?
      

  5.   

    每次出现这个错误:信息: Loading XML bean definitions from class path resource [beans.xml]
    错误开始:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hello' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'strAge' of bean class [zhao.WelHello]: Bean property 'strAge' is not writable or has an invalid setter method. Did you mean 'strAage'?我觉得很纳闷,程序这么写的!src目录底下有个beans.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="hello" class="zhao.WelHello">
            <property name="strAge" value="Spring"/>
        </bean>
    </beans>WelHello.java里有如下代码:package zhao;public class WelHello/* implements IHello*/ {
    private String strAge; public String update( String str ) {
    // TODO 自动生成方法存根
    return str+strAge;
    } public String getStrAage() {
    return strAge;
    } public void setStrAage(String strAage) {
    this.strAge = strAage;
    }
    }
    main方法里这么启动容器的:XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));那里有问题吗??谢谢了
      

  6.   

    顺便问一下我做的不是web项目,是普通的java项目,必要加进web.xml文件吗?要家的话是不是在项目的根目录底下?
      

  7.   

    你的这个WelHello写的有错
    看看这些地方
    package   zhao; public   class   WelHello/*   implements   IHello*/   { 
    private   String   strAge; public   String   update(   String   str   )   { 
    //   TODO   自动生成方法存根 
    return   str+strAge; 
    } public   String   getStrAge()   { 
    return   strAge; 
    } public   void   setStrAge(String   strAge)   { 
    this.strAge   =   strAge


    你对照红色标记的地方看一下,就知道了
      

  8.   

    ls说的对
    不过别忘记修改<beans> 
            <bean   id="hello"   class="zhao.WelHello"> 
                    <property   name="strAage"   value="Spring"   /> 
            </bean> 
    </beans> 
      

  9.   

    上面的兄弟,看到你的留言后我很....
    其实,开始写的时候我写strAage属性的时候写错了,然后自动生成set和get方法之后我把属性改为strAge,所以....
    反正挺谢谢你的,我以后得多细心一下~