New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a functional way to register a bean [SPR-14832] #19398
Comments
Juergen Hoeller commented Nice coincidence: This is on our 5.0 roadmap already :-) We have a few related tickets but none as specific this one yet, so I'll schedule it alongside the existing ones. |
Sébastien Deleuze commented I have a non trivial draft prototype that seems to work pretty well in Kotlin: context = GenericApplicationContext()
context.environment.addPropertySource("application.properties")
val mongoUri = context.environment.getProperty("mongo.uri")
val mongoDatabase = mongoUri.split("/")[3]
context.registerBean(IfEqHelperSource::class)
context.registerBean("messageSource", Supplier {
val messageSource = ResourceBundleMessageSource()
messageSource.setBasename("messages")
messageSource
})
context.registerBean(Supplier {
val viewResolver = HandlebarsViewResolver()
viewResolver.setPrefix("/templates/")
viewResolver
})
context.registerBean(LoggingEventListener::class)
context.registerBean(Supplier { MongoClients.create(mongoUri) })
context.registerBean(Supplier { ReactiveMongoTemplate(context.getBean(MongoClient::class), mongoDatabase) })
context.registerBean(UserRepository::class)
context.registerBean(UserController::class)
context.registerBean(GlobalController::class)
context.registerBean(TomcatServer::class)
context.refresh() Java version could be similar with an additional |
Oliver Drotbohm commented Looks like this is one more use case where type resolution of lambdas (as asked for in #18273) would come in handy as you'll probably want to be able to write: context.registerBean(() -> new MyComponent()); and it's hard to tell users why this doesn't work, or requires stating |
Sergei Egorov commented Could you please use CompletableFuture instead of Supplier? Will make it possible to do semi-parallel beans resolve. Pretty please! |
Sergei Egorov commented Very naive demo, but I think you'll get the point: Here both Connection and CachingTemplate are expensive beans (think Flyway migration before we return the DB template, maybe some cache warmups or index creation) Thanks to CompletableFuture API we can build... errr... A very simple graph of the dependencies and resolve them in parallel by keeping the blocking API with .get(timeout) call. In our apps, the context creation is a very expensive operation and takes a lot of time because we can't easily parallelize the process. |
Sébastien Deleuze commented An open question I have is: should the mechanism to provide a bean be a pure function with something like |
Juergen Hoeller commented I've got the basic mechanism working, with direct
Now the remaining part is convenience methods for registering such beans, not having to deal with |
Sébastien Deleuze commented I have upgraded the Mix-IT project to use this new mechanism and it works perfectly :-) |
Juergen Hoeller commented I'm in the process of adding
The remaining bean definition metadata gets derived from annotations on the component class, if any: including the scope, lazy-init flag, primary flag, qualifiers. This is a pretty convenient and pragmatic combination, not requiring extra APIs for those metadata attributes. I'm also adding variants with a constructor arguments Beyond the above, I envision a |
Sébastien Deleuze commented Sounds awesome! |
Juergen Hoeller commented I've revised the above signatures with a
They are actually declared on Given that arrangement, I don't see a need for a |
Juergen Hoeller commented Let's consider this ticket as done for M4 (aside from potential polishing until next week) and create a new JIRA ticket for a |
Yevhenii Melnyk commented Why not doing method which supplier is a subclass of the bean class? |
Juergen Hoeller commented For type checks in advance of singleton creation - and type checks in general for non-singleton beans - the provided |
Yevhenii Melnyk commented The idea of generic bean class has came to me while I was trying to use |
Hans Desmet opened SPR-14832 and commented
Add a registerBean method to ApplicationContext which accepts a lambda with which you register as bean.
The following code uses this method to register a bean of a class A and to register a
bean of class B which has a dependency on class A
Affects: 5.0 M2
Issue Links:
@Configuration
interface with Java 8 default methods (as a standalone artifact)1 votes, 8 watchers
The text was updated successfully, but these errors were encountered: