2017-06-22 68 views
0

我需要使用基於xml的依賴注入將java.sql.Time對象注入到Subject bean中。將java.sql.Time對象注入bean - spring xml dependency injection

這是我的Subject類定義。

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

在Java代碼中,這將是實現它的方法。

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

但現在我需要做同樣的注射是通過XML Time對象在Subject

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

我一直在尋找在互聯網上一段時間,但還沒有找到任何例子這個。只要一通過格式化字符串"yyyy-MM-dd"轉換爲使用SimpleDateFormat.parse("yyyy-MM-dd")

這讓我覺得應該有一個轉換到String對象Time類似的方式Date對象注入Date財產成Customer對象。這是我發現的例子。

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

順便說一句,the link to the above example

回答

1

執行從字符串轉換爲時間在你的對象。

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> 
+0

這似乎是另一種有效的方式來做到這一點。感謝您的回答 – Sandoval0992

0

這個問題的第一個正確答案。

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