2017-06-22 66 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>