关于简单的spring3.x的自动装载autowire
不能自动加载。我如果给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 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()); }}