2012-06-25 10 views
8

Considerate questo semplice esempio -Come inizializzare un oggetto Data Java nel file di configurazione xml di Spring?

public class Person 
{ 
    private String name; 
    private Date dateOfBirth; 

    // getters and setters here... 
} 

per inizializzare persona come un fagiolo primavera, posso scrivere quanto segue.

<bean id = "Michael" class = "com.sampleDomainName.Person"> 
<property name = "name" value = "Michael" /> 
</bean> 

Ma nella definizione di bean precedente, come posso impostare dateOfBirth?

Ad es. Voglio impostare il dateOfBirth come

1998-05-07 

risposta

2

Una delle risposte menzionate qui è utile, ma necessita di ulteriori informazioni. Devono essere forniti gli argomenti del costruttore per CustomDateEditor.

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
    <property name="customEditors"> 
    <map> 
     <entry key="java.util.Date"> <ref local = "customDateEditor" /> 
     </entry> 
    </map> 
    </property> 
</bean> 

<bean id = "customDateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> 
    <constructor-arg> 
     <bean class="java.text.SimpleDateFormat"> 
      <constructor-arg value="yyyy-MM-dd" /> 
     </bean> 
    </constructor-arg> 
    <constructor-arg value="true" /> 
</bean> 

Ora possiamo fare

<property name="dateOfBirth" value="1998-05-07" /> 
7

trattarlo come qualsiasi altro POJO (che si è)

<property name="dateOfBirth"> 
    <bean class="java.util.Date" /> 
</property> 

Se è necessario utilizzare un valore esplicito (come ad esempio 1975/04/10), quindi chiama semplicemente uno degli altri costruttori (anche se quelli che prendono anno-mese-giorno sono deprecati). È anche possibile utilizzare un java.beans.PropertyEditor esplicito che Spring rolls with already (vedere la sezione 6.4.2; si noti che è possibile scrivere i propri editor e registrarli per i propri tipi). È necessario registrare il CustomEditorConfigurer nella configurazione:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
    <property name="customEditors"> 
    <map> 
     <entry key="java.util.Date" 
      value="org.springframework.beans.propertyeditors.CustomDateEditor"/> 
    </map> 
    </property> 
</bean> 

Poi i dati assomiglia:

<property name="dateOfBirth" value="1975-04-10" /> 

Potrei aggiungere che non è Dateun adeguato tipo di dati per memorizzare un data- di nascita perché Date è in realtà un in tempo reale. Potrebbe essere utile guardare Joda e utilizzare la classe LocalDate.

+0

Probabilmente vuole inizializzarlo a una data specifica non in orario corrente. –

-1

Spring inject Date into bean property – CustomDateEditor

Questa carta dà due suggerimenti:

  1. fabbrica di fagioli
  2. CustomDateEditor

Suggerisco "Factory Bean" perché CustomDateEditor non è supportato in Spring 4.0+ e Factory Bean è abbastanza facile da usare.

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

Puoi includere alcune nozioni di base su cosa copre il link? Altrimenti se questo collegamento è irraggiungibile in futuro, questa risposta non comunicherà più nulla. –

+0

@DanLowe Ciao, ho aggiunto un esempio dal link che ho suggerito – nkduqi

Problemi correlati