spring注入bean的时候如何在xml中表示Calendar类型

看到网上有人问
[quote]bean中包含一个Calendar类型的变量。
想使用spring的配置文件来注入数据。
但是怎么在xml文件中表示Calendar数据呢?
请指教。[/quote]

我来写一下我的做法,用Spring 2.0实现,我首先想到的是两种注入方式,构造注入或是Settter注入,由于Calendar是抽象类,不能直接new对象,所以考虑用java.util.GregorianCalendar子类来实现,父引用装子对象。

下面看看我的实现:
先定义一个Teacher类,里面有三个属性,name,age,birthday.

Teacher.java

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;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "姓名:"+name+"\t年龄:"+age+"\t生日:"+getDate();
}
}


applicationContext.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"
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" class="java.util.GregorianCalendar">
<constructor-arg index="0" value="1989"/>
<constructor-arg index="1" value="4"/>
<constructor-arg index="2" value="1"/>
</bean>
<!--利用Teacher类中的Setter方式注入-->
<bean id="teacherBean" class="com.he.spring.beans.Teacher">
<property name="name" 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 stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Teacher teacher=(Teacher)context.getBean("teacherBean");
System.out.println(teacher);
}

}



当时也想到了利用Calendar的工厂方法getInstance(),再用setTime(Date date)方法Setter注入,这个方法就需要一个Date对象,考虑生成一个Date对象,但是Date对象的有参构造过时了,所以就放弃了,想用的可以参考

applicationContext.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"
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" class="java.util.Date">
<constructor-arg index="0" value="89"/>
<constructor-arg index="1" value="4"/>
<constructor-arg index="2" value="1"/>
</bean>

<bean id="calendarBean" class="java.util.Calendar" factory-method="getInstance">
<property name="time" ref="dateBean"/>
</bean>

<bean id="teacherBean" class="com.he.spring.beans.Teacher" autowire="byType">
<property name="name" value="张三"/>
<property name="age" value="35"/>
</bean>

</beans>


第一次发帖,我也正在学习Spring,遇到了这个问题,简单的解决了一下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值