读书人

spring注入bean的时候怎么在xml中表示

发布时间: 2012-09-13 09:51:53 作者: rapoo

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);}}



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

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"><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>


第一次发帖,我也正在学习Spring,遇到了这个问题,简单的解决了一下。 1 楼 lzk562209141 2010-10-15 写的很到位,学习……支持楼主!

读书人网 >XML SOAP

热点推荐