spring注入bean的时候如何在xml中表示Calendar类型
看到网上有人问
package com.he.spring.beans;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Teacher {private String name;private int age;private Calendar birthday;public Teacher() {super();// TODO Auto-generated constructor stub}public Teacher(String name, int age, Calendar birthday) {super();this.name = name;this.age = age;this.birthday = birthday;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Calendar getBirthday() {return birthday;}public void setBirthday(Calendar birthday) {this.birthday = birthday;}/** * @return */public String getDate(){Date date = birthday.getTime();SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");String dateStr = format.format(date);return dateStr;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn "姓名:"+name+"\t年龄:"+age+"\t生日:"+getDate();}}
applicationContext.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!--把属性值利用构造注入其中,注入的时间是"1989-05-01"--><bean id="gregorianCalendarBean" value="1989"/><constructor-arg index="1" value="4"/><constructor-arg index="2" value="1"/></bean><!--利用Teacher类中的Setter方式注入--><bean id="teacherBean" value="张三"/><property name="age" value="35"/><property name="birthday" ref="gregorianCalendarBean"/></bean></beans>
package com.he.spring.demo;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.he.spring.beans.Teacher;public class TeacherDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Teacher teacher=(Teacher)context.getBean("teacherBean");System.out.println(teacher);}}<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="dateBean" value="89"/><constructor-arg index="1" value="4"/><constructor-arg index="2" value="1"/></bean><bean id="calendarBean" factory-method="getInstance"><property name="time" ref="dateBean"/></bean><bean id="teacherBean" autowire="byType"><property name="name" value="张三"/><property name="age" value="35"/></bean></beans>