Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:47:52+00:00 2026-05-20T04:47:52+00:00

springframework.beans.BeanUtils is very useful to copy objects, and I use the ignoreProperties option frequently.

  • 0

springframework.beans.BeanUtils is very useful to copy objects, and I use the “ignoreProperties” option frequently. However, sometimes I want to copy only specific objects (basically, the opposite of “ignore Properties”). Does anyone know how can I do that? Any help will be appreciated.

import org.springframework.beans.BeanUtils;

public class Sample {    
    public static void main(String[] args) {    
        DemoADto demoADto = new DemoADto();
        demoADto.setName("Name of Demo A");
        demoADto.setAddress("Address of Demo A");

        DemoBDto demoBDto = new DemoBDto();

        // This is "ignoreProperties" option
        // But I want to know how I can copy only name field by using BeanUtils or something.
        BeanUtils.copyProperties(demoADto, demoBDto, new String[] {"address"});

        System.out.println(demoBDto.getName());
        System.out.println(demoBDto.getAddress());    
    }    
}

public class DemoADto {    
    private String name;    
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }    
}

public class DemoBDto {    
    private String name;    
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:47:53+00:00Added an answer on May 20, 2026 at 4:47 am

    You can use the BeanWrapper technology. Here’s a sample implementation:

    public static void copyProperties(Object src, Object trg, Iterable<String> props) {
    
        BeanWrapper srcWrap = PropertyAccessorFactory.forBeanPropertyAccess(src);
        BeanWrapper trgWrap = PropertyAccessorFactory.forBeanPropertyAccess(trg);
    
        props.forEach(p -> trgWrap.setPropertyValue(p, srcWrap.getPropertyValue(p)));
    
    }
    

    Or, if you really, really want to use BeanUtils, here’s a solution. Invert the logic, gather excludes by comparing the full property list with the includes:

    public static void copyProperties2(Object src, Object trg, Set<String> props) {
        String[] excludedProperties = 
                Arrays.stream(BeanUtils.getPropertyDescriptors(src.getClass()))
                      .map(PropertyDescriptor::getName)
                      .filter(name -> !props.contains(name))
                      .toArray(String[]::new);
    
        BeanUtils.copyProperties(src, trg, excludedProperties);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting... org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You cannot use a spring-security-2.0.xsd schema with Spring
I'm having the following issue with my hibernate 3.6.10 project: org.springframework.beans.factory.BeanCreationException: Error creating bean
Does anybody recognize this error? org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory': Post-processing of
below is a sample class: AAAA.java 1 package tp.domain; 2 3 import org.springframework.beans.factory.annotation.Value; 4
I have the following in my dispatcher-servlet.xml <?xml version=1.0 encoding=UTF-8?> <beans xmlns=http://www.springframework.org/schema/beans xmlns:mvc=http://www.springframework.org/schema/mvc xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
This is working: <?xml version=1.0 encoding=UTF-8?> <beans:beans xmlns=http://www.springframework.org/schema/security xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:beans=http://www.springframework.org/schema/beans xmlns:context=http://www.springframework.org/schema/context xsi:schemaLocation=http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
I want to have two EntityManagerFactories ( org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in this case), with each of
I've got a question regarding the difference between PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) and normal filters defined
I am a Spring user and I'm quite fond of things like org.springframework.beans.factory.annotation.Value and
We use the code below to inject Spring beans with properties from a properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.