不能自动加载。我如果给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
请指教。
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
请指教。
解决方案 »
- 求教
- struts2怎么获得上一个请求的url中参数
- 为什么我fedora12下yum安装tomcat6启动会失败?
- 紧急求助:用JDBC调用存储过程
- 请教Spring MVC的SimpleFormController控制器里,如何在referenceDate()方法中准备表单需要的1-4级联动下拉框数据?
- 关于j2ee程序发布的问题(急,请高手指点)
- 小女子真心求教
- 有谁改造过shark开源工作流引擎吗?30万行代码,如何着手呢.
- jbuilder9中如何配置j2ee和Weblogic?
- 哪儿能下载到《JAVA 与模式》?
- 请教:SSH2如何利用aop拦截action做日志记录?
- 求这个Java EE期末考试试题
<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
你贴出来的东西,除了HelloWorld少无参构建器之外,其他并没有问题。
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>