Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to declare a Java 8 method reference in a Spring XML file?

I would like to declare a Java 8 method reference as a Spring bean. What is the easiest way of doing this in a Spring XML file?

For example, suppose I have:

class Foo {
    Foo(ToLongFunction<Bar> fn) { ... }
}

class Bar {
    long getSize() { ... }
}

... and I want to create a Foo that takes the method reference Bar::getSize as the constructor argument.

How do I declare the Foo instance in a Spring bean XML file?

like image 984
Kkkev Avatar asked Jun 04 '15 18:06

Kkkev


People also ask

What is Ref tag in Spring XML?

Spring Bean Reference ,<ref> Tag in Spring In Spring we need to use <ref> element to inform spring container about the object dependency. In Spring, beans can "access" to each other by specify the bean references in the same or different bean configuration file.In spring we can write multiple configuration xml file.

How do you reference a method using parameters?

To make the code clearer, you can turn that lambda expression into a method reference: Consumer<String> c = System. out::println; In a method reference, you place the object (or class) that contains the method before the :: operator and the name of the method after it without arguments.

What is Java 8 method reference?

Java provides a new feature called method reference in Java 8. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.


1 Answers

My proposed solution below is probably not the best idea, but I found the question interesting and decided to try to give it a shot. This is the best I could come up with.

I don't know if there is a way to do this directly in this moment (aside from defining some kind of factory bean), but alternatively you could do it using dynamic language support, for instance with Groovy.

The following example ran for me using the latest version of Spring (as of today 4.1.6)

Supposing a bean like this

public class Foo {

    private Function<String, String> task;

    @Autowired
    public Foo(Function<String, String> task){
        this.task = task;
    }

    public void print(String message) {
        System.out.println(task.apply(message));    
    }

}

Then I could define an XML configuration like:

<lang:groovy id="func">
    <lang:inline-script>
        <![CDATA[
                import java.util.function.Function
                { text -> "Hello " + text } as Function
        ]]>
    </lang:inline-script>
</lang:groovy>

<bean id="foo" class="demo.services.Foo">
    <constructor-arg name="task" ref="func"/>
</bean>

Of course, the syntax of your lambda will depend on the language you choose. I have no idea if Groovy has something like method reference, but any method reference could be expressed with a lambda/closure as I did above.

like image 197
Edwin Dalorzo Avatar answered Oct 21 '22 23:10

Edwin Dalorzo