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 805867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:09:24+00:00 2026-05-15T00:09:24+00:00

I have an API which I am turning into an internal DSL. As such,

  • 0

I have an API which I am turning into an internal DSL. As such, most methods in my PoJos return a reference to this so that I can chain methods together declaratively as such (syntactic sugar).

myComponent
    .setID("MyId")
    .setProperty("One")
    .setProperty2("Two")
    .setAssociation(anotherComponent)
    .execute();

My API does not depend on Spring but I wish to make it ‘Spring-Friendly’ by being PoJo friendly with zero argument constructors, getters and setters. The problem is that Spring seems to not detect my setter methods when I have a non-void return type.

The return type of this is very convenient when chaining together my commands so I don’t want to destroy my programmatic API just be to compatible with Spring injection.

Is there a setting in Spring to allow me to use non-void setters?

Chris

  • 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-15T00:09:24+00:00Added an answer on May 15, 2026 at 12:09 am

    Thanks to all (and especially Espen who went to a lot of effort to show me the various options within Spring).

    In the end, I found a solution myself that doesn’t require Spring configuration.

    I followed the link from Stephen C then found a reference to the SimpleBeanInfo class within that set of Threads. This class allows a user to write their own bean method resolution code by placing another class in the same package as the class with the non-standard setters/getters to override the logic of with ‘BeanInfo’ appended onto the classname and implementing the ‘BeanInfo’ interface.

    I then did a search on Google and found this blog which pointed the way. The solution on the blog was quite basic so I padded it out for my purposes.

    Per Class (with fluent setters)

    public class MyComponentBeanInfo<T> extends SimpleBeanInfo {
    
    private final static Class<?> _clazz = MyComponent.class;
    PropertyDescriptor[] _properties = null;
    
    public synchronized PropertyDescriptor[] getPropertyDescriptors() {
        if (_properties == null) {
            _properties = Helpers.getPropertyDescriptionsIncludingFluentSetters(_clazz);
        }
        return _properties;
    }
    
    public BeanDescriptor getBeanDescriptor() {
        return new BeanDescriptor(_clazz);
    }
    }
    

    PropertyDescriptor generation method

    public static PropertyDescriptor[] getPropertyDescriptionsIncludingFluentSetters( Class<?> clazz) {
        Map<String,Method> getterMethodMap = new HashMap<String,Method>();
        Map<String,Method> setterMethodMap = new HashMap<String,Method>();
        Set<String> allProperties = new HashSet<String>();
        PropertyDescriptor[] properties = null;
        try {
            Method[] methods = clazz.getMethods();
            for (Method m : methods) {
                String name = m.getName();
                boolean isSetter = m.getParameterTypes().length == 1 && name.length() > 3 && name.substring(0,3).equals("set") && name.charAt(3) >= 'A' && name.charAt(3) <= 'Z';
                boolean isGetter = (!isSetter) && m.getParameterTypes().length == 0 && name.length() > 3 && name.substring(0,3).equals("get") && name.charAt(3) >= 'A' && name.charAt(3) <= 'Z';
    
                if (isSetter || isGetter) {
                    name = name.substring(3);
                    name = name.length() > 1
                            ? name.substring(0,1).toLowerCase() + name.substring(1)
                            : name.toLowerCase();
    
                    if (isSetter) {
                        setterMethodMap.put(name, m);
                    } else {
                        getterMethodMap.put(name, m);
                    }
                    allProperties.add(name);
                }
            }
    
            properties = new PropertyDescriptor[allProperties.size()];
            Iterator<String> iterator = allProperties.iterator();
            for (int i=0; i < allProperties.size(); i++) {
                String propertyName = iterator.next();
                Method readMethod = getterMethodMap.get(propertyName);
                Method writeMethod = setterMethodMap.get(propertyName);
                properties[i] = new PropertyDescriptor(propertyName, readMethod, writeMethod);
            }
        } catch (IntrospectionException e) {
            throw new RuntimeException(e.toString(), e);
        }
        return properties;
    }
    

    Advantages to this approach:

    • No custom spring configuration (Spring is not aware of the non-standard setters and sees them as normal). No dependancy on any Spring .jar files but accessible from Spring.
    • Just seems to work.

    Disadvantages to this approach:

    • I have to place create a BeanInfo class for all of my API classes with non-standard setters. Luckily there are only around 10 such classes and by moving the method resolution logic into a seperate class I only have one place to maintain.

    Closing Thoughts

    In my opinion, Spring should deal with fluent setters natively, they don’t hurt anyone and it should just ignore the return value.

    By requiring that setters be rigidly void, it has forced me to write a lot more boiler plate code than I would have needed otherwise. I appreciate the Bean Specification, but bean resolution is trivial using reflection without even using the standard bean resolver so Spring should offer the option of its own bean resolver that will handle this situations.

    By all means, leave the standard mechanism as the default, but offer a one-line configuration option. I look forward to future versions where this might be optionally relaxed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this syntax which works (since it's from the API, pretty much) <%
I have this function: RegisterGlobalHotKey(Keys.F6, MOD_SHIFT | MOD_CONTROL); which call an API to register
We have a REST API which clients routinely POST and PUT data to. When
Is it possible to have a C static library API, which uses C++ internally
I have an API that is dependent on certain state information between requests. As
So Google Analytics does not have an API that we can use to get
So, I have an API that I need to implement in to an existing
I have an API consisting of ASP.NET webservices callable through GET/POST/SOAP. A new functionality
I have an API call in my application where I am checking the time
If you have an API, and you are a UK-based developer with a highly

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.