Using a Java8 Lambda Function inside spring XML Using a Java8 Lambda Function inside spring XML spring spring

Using a Java8 Lambda Function inside spring XML


I think what you need to do is to switch to Java Config (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java)In the meantime, if you can't switch, you can still pull it off by combining methods. In you're XML configuration file add:

<bean class="com.myapp.config.FooBarConfiguration"/>

Then, you create class com.myapp.config.FooBarConfiguration like this:

@Configurationpublic class FooBarConfiguration {    @Bean    public FooToBarTransformer fooTransformer() {        return new FooToBarTransformer();    }    @Bean    public Thing theThing(FooToBarTransformer fooTransformer) {        return new Thing(fooTransformer::transform);    }}

Just make sure that ConfigurationClassPostProcessor is a registered post-processor in your Spring context by either having:

<context:annotation-config>

Or

<context:component-scan>

Or by manually adding the post processor as a bean:

<bean class="org.springframework.context.annotation.ConfigurationClassPostProcessor"/>

This should work from Spring 3.0 and above according with http://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/context/annotation/ConfigurationClassPostProcessor.html. Not sure what version you're using.


Try to use factory pattern. See org.springframework.beans.factory.FactoryBean.Also spring has expression language.