Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Spring In Action 5th Edition Question  RSS feed

 
Ranch Hand
Posts: 440
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does Spring 5 allow you to inject a lambda into a bean via configuration?
 
author
Posts: 396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It depends on how you do configuration, but yes. Certainly, there's no way to do it with XML config. But if you are using JavaConfig and the bean you're creating requires a functional type, then there's no reason you couldn't just pass a lambda or method reference into the setter or constructor arg.

On the other hand, if you're asking if it's possible to declare a lambda as a bean, then I'm not so sure. I'd wonder what that would even look like. i suppose that it'd be possible to manually register a lambda as a bean by calling registerBean() on a given GenericApplicationContext, but I can't say that I've tried it. (I have used Lambdas to define a bean of a given type when calling registerBean(), but never have had the bean itself be defined as a Lambda.)


Will Myers wrote:Hi,
Does Spring 5 allow you to inject a lambda into a bean via configuration?

 
Ranch Hand
Posts: 196
1
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never personally used, but I have seen some examples around ...

Spring 4 on Java 8


 
meenakshi sundar
Ranch Hand
Posts: 196
1
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another interesting read on the functional web framework...

functional-web-framework
 
Craig Walls
author
Posts: 396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, this is a good example, I often use lambdas or method references in place of a RowMapper when working with JdbcTemplate (in fact, I cover this in the book). I wasn't thinking of that use of lambdas as being DI, though...although I guess you could say that they are. I was thinking more of how you'd inject a lambda in the context of a configuration class.


meenakshi sundar wrote:I have never personally used, but I have seen some examples around ...

Spring 4 on Java 8


 
Craig Walls
author
Posts: 396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, another great example of using functions (lambdas or method references) in Spring. In fact, I cover this in the book, as well.

meenakshi sundar wrote:Another interesting read on the functional web framework...

functional-web-framework

 
What's gotten into you? Could it be this tiny ad?
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!