不能自动加载。我如果给bean加一个构造函数,设置autowire="constructor"的时候可以,但是去掉构造函数,设置为autowire="byName"或者autowire="byType"的时候,不能自动加载,在控制台输出为null。下面是代码bean代码:package com.gc.action;import java.util.Date;
public class HelloWorld {
public String msg=null; public Date date=null;

public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public HelloWorld(Date date){
this.date=date;
}
}
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="HelloWorld" class="com.gc.action.HelloWorld" autowire="byName">
<property name="msg">
<value>1111</value>
</property>
</bean>
<bean id="date" class="java.util.Date"></bean>
</beans>测试代码:
package com.gc.test;import java.io.FileNotFoundException;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;import com.gc.action.HelloWorld;public class TestHelloWorld { /**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloWorld=(HelloWorld)actx.getBean("HelloWorld");
System.out.println(helloWorld.getDate()+":"+helloWorld.getMsg());

}}
输出:
null:1111
请指教。

解决方案 »

  1.   

    这个是bean的自动加载......  你至少要保证spring里面有你所对应名称的bean撒  意思就是msg和date的bean撒
      

  2.   

    msg的属性我已经显式设定了,date的bean不就在下面吗
      

  3.   

    <bean name="test" class="com.Test" autowire="byName"/>
    <bean name="date" class="java.util.Date"></bean>public class Test { private Date date;
    public Date getDate() {
    return date;
    } public void setDate(Date date) {
    this.date = date;
    }
    public static void main(String[] args) throws FileNotFoundException {
            // TODO Auto-generated method stub
            ApplicationContext actx=new FileSystemXmlApplicationContext(new String[]{"D:\\workspace\\cloudserver\\src\\com\\spring_action.xml"});
            Test helloWorld=(Test)actx.getBean("test");
            System.out.println(helloWorld.getDate());
            
        }}没问题啊,输出:
    Thu Jan 05 10:29:51 CST 2012
      

  4.   

    是这么配的。
    你贴出来的东西,除了HelloWorld少无参构建器之外,其他并没有问题。
      

  5.   

    晕是不是因为是spring的版本问题啊,我的是2.5的
      

  6.   

    我刚又用myeclipse搭建了个spring2.5的测试了一下,也没有问题.....
      

  7.   

    package com;import java.io.FileNotFoundException;
    import java.util.Date;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;public class Test { private String msg = null;
    private Date date = null;

    public String getMsg() {
    return msg;
    } public void setMsg(String msg) {
    this.msg = msg;
    } public Date getDate() {
    return date;
    } public void setDate(Date date) {
    this.date = date;
    }
    public static void main(String[] args) throws FileNotFoundException {
            // TODO Auto-generated method stub
            ApplicationContext actx=new FileSystemXmlApplicationContext(new String[]{"D:\\workspace\\asdf\\src\\applicationContext.xml"});
            Test helloWorld=(Test)actx.getBean("test");
            System.out.println(helloWorld.getDate() + "  :  " + helloWorld.getMsg());
            
        }}<?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-2.5.xsd"> <bean name="test" class="com.Test" autowire="byName">
    <property name="msg">
    <value>123</value>
    </property>
    </bean>
    <bean name="date" class="java.util.Date"></bean>
    </beans>