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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:50:33+00:00 2026-05-15T21:50:33+00:00

I would like to be able to use Spring using setter injection into Scala

  • 0

I would like to be able to use Spring using setter injection into Scala components. Unfortunately, Scala’s native setters are named differently from than the JavaBeans standard, foo_= rather than setFoo. Scala does provide a couple of workarounds for this, annotations which force the creation of JavaBeans setters/getters as well as native Scala ones, but that requires annotating every component that I wish to inject. Much more convenient would be to override the BeanWrapper used by Spring with one which knew how to handle Scala-style getters and setters.

There doesn’t appear to be any documentation on how to do such a thing or whether it is even feasible, nor any online examples of anyone else doing it. So before diving into the source, I figured I’d check here

  • 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-15T21:50:34+00:00Added an answer on May 15, 2026 at 9:50 pm

    It looks like AbstractAutowireCapableBeanFactory (where most of the work with BeanWrapper is done) is hardcoded to use BeanWrapperImpl. No point of extension there. BeanWrapperImpl uses CachedIntrospectionResults which uses Introspector in turn. Looks like there is no way to configure any of these dependencies. We can try to use standard points of extension: BeanPostProcessor or BeanFactoryPostProcessor.

    Using just BeanPostProcessor will not work, because if we are doing something like this:

    <bean id="beanForInjection" class="com.test.BeanForInjection">
        <property name="bean" ref="beanToBeInjected"/>        
    </bean>
    

    where BeanForInjection is a Scala class

    package com.test
    import com.other.BeanToBeInjected
    
    class BeanForInjection {
        var bean : BeanToBeInjected = null;
    }
    

    and BeanToBeInjected is a bean we want to inject, then we will catch exception before BeanPostProcessor will have a chance to step in. Beans gets populated with values before any callbacks of BeanPostProcessor called.

    But we can use BeanFactoryPostProcessor to ‘hide’ properties that are expected to be injected through Scala-like setters and apply them latter.

    Something lilke this:

    package com.other;
    
    import ...
    
    public class ScalaAwareBeanFactoryPostProcessor implements BeanFactoryPostProcessor, PriorityOrdered {
    
        ... PriorityOrdered related methods...
    
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            String[] beanNames = beanFactory.getBeanDefinitionNames();
            for (String currentName : beanNames) {
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(currentName);
                processScalaProperties(beanDefinition);
            }
        }
    
        protected void processScalaProperties(BeanDefinition beanDefinition) {
            String className = beanDefinition.getBeanClassName();
            try {
                Set<PropertyValue> scalaProperties = new HashSet<PropertyValue>();
                for (PropertyValue propertyValue : beanDefinition.getPropertyValues().getPropertyValueList()) {
                    String scalaSetterName = ScalaAwarePostProcessorUtils.getScalaSetterName(propertyValue.getName());
    
                    BeanInfo beanInfo = getBeanInfo(className);
                    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                    MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
                    for (MethodDescriptor md : methodDescriptors) {
    
                        if (scalaSetterName.equals(md.getName())) {
                            boolean isScalaProperty = true;
                            for (PropertyDescriptor pd : propertyDescriptors) {
                                if (propertyValue.getName().equals(pd.getName())) {
                                    isScalaProperty = false;
                                }
                            }
                            if (isScalaProperty) {
                                scalaProperties.add(propertyValue);
                            }
                        }
                    }
                }
    
                if (!scalaProperties.isEmpty()) {
                    beanDefinition.setAttribute(ScalaAwarePostProcessorUtils.SCALA_ATTRIBUTES_KEY, scalaProperties);
                }
    
                for (PropertyValue propertyValue : scalaProperties) {
                    beanDefinition.getPropertyValues().removePropertyValue(propertyValue);
                }
            } catch (ClassNotFoundException e) {
            } catch (IntrospectionException e) {
            }
        }
    
        private BeanInfo getBeanInfo(String className) throws ClassNotFoundException, IntrospectionException {
            Class beanClass = Class.forName(className);
            BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
            cleanIntrospectorCache(beanClass);
            return beanInfo;
        }
    
        private void cleanIntrospectorCache(Class beanClass) {
            Class classToFlush = beanClass;
            do {
                Introspector.flushFromCaches(classToFlush);
                classToFlush = classToFlush.getSuperclass();
            }
            while (classToFlush != null);
        }
    }
    

    This implementation simply checks is any bean has properties that are not listed as properties and also have Scala-like setters. All properties that match this contract are removed from properties list and saved as attributes of the bean. Now, all we need is to pull this attributes (if any) for every bean and apply them. There is where we need BeanPostProcessor (AutowiredAnnotationBeanPostProcessor can be a good example of BeanPostProcessor).

    package com.other;
    
    public class ScalaAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
        implements PriorityOrdered, BeanFactoryAware {
    
        private ConfigurableListableBeanFactory beanFactory;
    
        ... Order related stuff...
    
        public void setBeanFactory(BeanFactory beanFactory) {
            if (beanFactory instanceof ConfigurableListableBeanFactory) {
                this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
            }
        }
    
        @Override
        public PropertyValues postProcessPropertyValues(PropertyValues pvs,     PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
            try {
                InjectionMetadata metadata = findScalaMetadata(beanFactory.getBeanDefinition(beanName), bean.getClass());
                metadata.inject(bean, beanName, pvs);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(beanName, "Injection of Scala dependencies failed", ex);
            }
            return pvs;
        }
    
        private InjectionMetadata findScalaMetadata(BeanDefinition beanDefinition, Class<?> beanClass) throws IntrospectionException {
            LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    
            Set<PropertyValue> scalaProperties = (Set<PropertyValue>) beanDefinition.getAttribute(ScalaAwarePostProcessorUtils.SCALA_ATTRIBUTES_KEY);
            if (scalaProperties != null) {
                for (PropertyValue pv : scalaProperties) {
                    Method setter = ScalaAwarePostProcessorUtils.getScalaSetterMethod(beanClass, pv.getName());
                    if (setter != null) {
                        Method getter = ScalaAwarePostProcessorUtils.getScalaGetterMethod(beanClass, pv.getName());
                        PropertyDescriptor pd = new PropertyDescriptor(pv.getName(), getter, setter);
                        elements.add(new ScalaSetterMethodElement(setter, pd));
                    }
                }
            }
            return new InjectionMetadata(beanClass, elements);
        }
    
        private class ScalaSetterMethodElement extends InjectionMetadata.InjectedElement {
    
            protected ScalaSetterMethodElement(Member member, PropertyDescriptor pd) {
                super(member, pd);
            }
    
            @Override
            protected Object getResourceToInject(Object target, String requestingBeanName) {
                Method method = (Method) this.member;
                MethodParameter methodParam = new MethodParameter(method, 0);
                DependencyDescriptor dd = new DependencyDescriptor(methodParam, true);
                return beanFactory.resolveDependency(dd, requestingBeanName);
            }
        }
    }
    

    Simply create these two beans in your context:

    <bean class="com.other.ScalaAwareBeanFactoryPostProcessor"/>
    
    <bean class="com.other.ScalaAwareBeanPostProcessor"/>
    

    Note:

    This is not a final solution. It will work for classes, but it will not work for simple types:

    <bean id="beanForInjection" class="com.test.BeanForInjection">
        <property name="bean" ref="beanToBeInjected"/>        
        <property name="name" value="skaffman"/>
    </bean>
    

    Solution will work for bean, but not for name. This can be fixed, but at this point I think you will be better off just using @BeanInfo annotation.

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

Sidebar

Related Questions

I am using Spring Security 3.0.6 and I would like to be able to
I would like to be able to use the same Session variable when transferring
I would like to be able to use the same drawable to represent both:
I would like to be able to use the created functions and objects I
I'm new to the iPhone and I would like to be able to use
This is really just for my own use: I would like to be able
I would like to be able to use DataAnnotations when working with my model,
I am using Spring to display messages from a properties file. I would like
I would like to share Session Attributes betweet two Controllers in Spring MVC, using
I am using a TreeView control, and I would like to be able to

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.