0

I need to inject a java.sql.Time object into a Subject bean using xml based dependency injection.

This is my Subject class definition.

public class Subject{ private java.sql.Time startedTime; } 

In Java code this will be the way to do it.

Subject subject = new Subject(); Time startedTime = Time.valueOf("HH:MM:SS"); subject.setStartedTime(startedTime); 

But now I need to do the same injecting that Time object in the Subject bean via xml

<bean id="startedTime" class="mx.com.project.Subject"> <property name="startedTime"> <!-- java.sql.Time injection--> </property> </bean> 

I've been looking for a while on the internet but have not found any example on this. Just one to inject a Date property into a Customer object by converting a formatted string "yyyy-MM-dd" to a Date object using SimpleDateFormat.parse("yyyy-MM-dd")

It makes me think there should be a similar way to convert a String to Time object. This is the example I found.

<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="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="customer" class="com.mkyong.common.Customer"> <property name="date"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2010-01-31" /> </bean> </property> </bean> </beans> 

By the way, the link to the above example

2 Answers 2

1

Perform the conversion from String to Time in your object.

public class Subject { private java.sql.Time startedTime; // blah. your stuff. public void setStartedTimeValue(final String startedTimeValue) { startedTime = Time.valueof(startedTimeValue); } } <bean id="startedTime" class="mx.com.project.Subject"> <property name="startedTimeValue" value="20:14:37"/> </bean> 
Sign up to request clarification or add additional context in comments.

1 Comment

It seems like that's another valid way to do it. Thanks for your answer
0

The first right answer to this question.

 <property name="startedTime"> <bean factory-method="valueOf" class="java.sql.Time"> <constructor-arg value="16:00:00" /> </bean> </property> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.