2013-11-02 14 views
12

Vorrei sapere come copiare le proprietà da un'origine oggetto a un oggetto Dest che ignora i valori nulli utilizzando Spring Framework.Come ignorare i valori nulli utilizzando springFramework BeanUtils copyProperties?

Io in realtà uso beanutils Apache, con questo codice

beanUtils.setExcludeNulls(true); 
    beanUtils.copyProperties(dest, source); 

per farlo. Ma ora ho bisogno di usare Spring.

Qualsiasi aiuto?

Thx un sacco

+0

si può non includere BeanUtils come parte della vostra primavera percorso di classe del progetto? Non credo che i BeanUtils di Spring funzionino in questo modo. –

risposta

31

È possibile creare il proprio metodo per copiare le proprietà ignorando i valori nulli.

versione
public static String[] getNullPropertyNames (Object source) { 
    final BeanWrapper src = new BeanWrapperImpl(source); 
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); 

    Set<String> emptyNames = new HashSet<String>(); 
    for(java.beans.PropertyDescriptor pd : pds) { 
     Object srcValue = src.getPropertyValue(pd.getName()); 
     if (srcValue == null) emptyNames.add(pd.getName()); 
    } 
    String[] result = new String[emptyNames.size()]; 
    return emptyNames.toArray(result); 
} 

// then use Spring BeanUtils to copy and ignore null 
public static void myCopyProperties(Object src, Object target) { 
    BeanUtils.copyProperties(src, target, getNullPropertyNames(src)) 
} 
+0

Questa è un'ottima soluzione per questo. Speravo che il Framework avesse già programmato un modo per occuparsene, ma funzionerebbe perfettamente. grazie – Fingolricks

+0

Ho fatto lo stesso. Ho incluso questi metodi in una nuova classe con alcune altre implementazioni personalizzate. La mia nuova classe estende la classe BeanUtils in modo da poter aggiungere funzionalità e funzionalità aggiuntive. – yuva

+0

C'è errore di battitura nel segnale di chiamata del metodo myCopyProps ... extra virgola dopo l'oggetto –

18

Java 8 del metodo di getNullPropertyNames dal di alfredx post:

public static String[] getNullPropertyNames(Object source) { 
    final BeanWrapper wrappedSource = new BeanWrapperImpl(source); 
    return Stream.of(wrappedSource.getPropertyDescriptors()) 
      .map(FeatureDescriptor::getName) 
      .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null) 
      .toArray(String[]::new); 
} 
3

SpringBeans.xml

<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-3.0.xsd"> 

    <bean id="source" class="com.core.HelloWorld"> 
     <property name="name" value="Source" /> 
     <property name="gender" value="Male" /> 
    </bean> 

    <bean id="target" class="com.core.HelloWorld"> 
     <property name="name" value="Target" /> 
    </bean> 

</beans> 
  1. Creare un Java Bean,

    classe pubblica HelloWorld {

    private String name; 
    private String gender; 
    
    public void printHello() { 
        System.out.println("Spring 3 : Hello ! " + name + " -> gender  -> " + gender); 
    } 
    

    // getter e setter

  2. Create Main Class per testare

    public class App { void main (String [] args) {public static ApplicationContext context = new ClassPathXmlApplicationContext (" SpringBeans.xml ");

    HelloWorld source = (HelloWorld) context.getBean("source"); 
        HelloWorld target = (HelloWorld) context.getBean("target"); 
    
        String[] nullPropertyNames = getNullPropertyNames(target); 
        BeanUtils.copyProperties(target,source,nullPropertyNames); 
        source.printHello(); 
    } 
    
    public static String[] getNullPropertyNames(Object source) { 
        final BeanWrapper wrappedSource = new BeanWrapperImpl(source); 
        return Stream.of(wrappedSource.getPropertyDescriptors()) 
          .map(FeatureDescriptor::getName) 
          .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null) 
          .toArray(String[]::new); 
    } 
    

    }

Problemi correlati