HelloBean 代码:
package study.spring;
import java.util.Date;
public class HelloBean {
private String helloword;
private Date date;
public String getHelloword() {
return helloword;
}
public void setHelloword(String helloword) {
this.helloword = helloword;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
Spring 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-2.5.xsd">
<bean id="dateBean" class="java.util.Date" />
<bean id="helloBean" class="study.spring.HelloBean" autowire="byType">
<property name="helloword" value="这个是一个byType的实例!" />
</bean>
</beans>
测试代码:
package study.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("file:\\BBS_Project\\autowrite\\src\\study\\spring\\peizhi.xml");
HelloBean hello=(HelloBean) context.getBean("helloBean");
System.out.println("写入内容为:"+hello.getHelloword());
System.out.println("时间:"+hello.getDate());
}
}
OK....JAVA APPLITION一下子,控制台给出结果:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
写入内容为:这个是一个byType的实例!
时间:null
我就奇怪了,为什么这里的时间为:null ???????????
------解决方案--------------------
在<bean id="helloBean" class="study.spring.HelloBean" autowire="byType"> 下面再加一句
<property name="date" ref="dateBean"/>