spring 自动装配Date为null

一次奇怪的事情,在为以下类实行自动装配时不成功:

public class Computer {
    private Date date;
       
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
        
}

xml配置为:

    <bean id="date" class="java.util.Date">
    </bean>
    <bean id="computer" class="com.zlc.test.Computer" autowire="byName">      
    </bean>
    

运行以下代码:

public static void main(String[] args) {
        String conf = "applicationContext.xml";
        //创建容器对象
        AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf); 
        Computer c = ac.getBean("computer",Computer.class);
        System.out.println(c.getDate());
        
    }

输出:

改为autowire="byType",运行结果也是一样的

 

posted @ 2018-08-02 09:11  梦里下起了雪  阅读(292)  评论(0编辑  收藏  举报