Preparation of Spring Boot module for Ratpack

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Preparation of Spring Boot module for Ratpack

Lari Hotari
Hi,

I'd like to contribute a module that embeds Spring Boot inside Ratpack. Spring beans are available to use in Ratpack autowiring.

The idea is not to replace the Guice module, but add a "joined registry" that gets dependencies from both Guice and Spring Boot .

I've implemented this proof-of-concept for my Ratpack and Grails 3 talk I had in Gr8Conf EU and US:
https://github.com/lhotari/ratpack-gorm-example
it currently requires my fork of ratpack: https://github.com/lhotari/ratpack

commits to add the Spring Boot module:
https://github.com/lhotari/ratpack/compare/v0.9.7...spring-boot

Before adding tests and doing a PR, I'd like to make sure that the way to extend ratpack-guice is correct.
The approach I've used is pretty ugly and there is a "FactoryFactory" class (GuiceBackedHandlerFactoryFactory). Any suggestions to improve this?

The SpringBackedRegistry class I have used is from https://github.com/dsyer/spring-boot-ratpack/blob/master/src/main/java/ratpack/spring/internal/SpringBackedRegistry.java and written by Dave Syer.

Lari
Reply | Threaded
Open this post in threaded view
|

Re: Preparation of Spring Boot module for Ratpack

Luke Daley
Administrator
Hi Lari,

I'd be happy to have a `ratpack-spring` module that bridges our Registry to an ApplicationContext.

We don't need to do as much as your code is doing though. I really don't want to do any service loader or classpath adaptive stuff.

We should consider the use cases here. That is, in what environments are people likely to want to use Ratpack and Spring? Seems to me that Spring Boot (and Grails) are likely candidates. If so, we should focus on using `ratpack-spring` when bootstrapping from these environments.
Reply | Threaded
Open this post in threaded view
|

Re: Preparation of Spring Boot module for Ratpack

Lari Hotari
The idea was to implement a Ratpack module and keep Ratpack in control of bootstrapping the application.  Dave Syer's has already implemented a module for embedding Ratpack inside Spring Boot. https://github.com/dsyer/spring-boot-ratpack 

The reason I had to do the dirty work in my solution was that I'd like to add a "joined registry" where it gets dependencies from both Guice and Spring Boot and where Guice isn't replaced.
So the first feature I'd like to implement is to make it possible to add multiple registries to Ratpack and keeping Guice also there. Does this make any sense?

Could we first clean up  "ratpack-core", "ratpack-groovy" and "ratpack-guice" code to make this (= "joined registry" without replacing ratpack-guice) possible?
 

Reply | Threaded
Open this post in threaded view
|

Re: Preparation of Spring Boot module for Ratpack

Luke Daley
Administrator
You can already join registries. Given the Spring based Registry impl, you’d just do this…

handlers {  
  register new ApplicationContextBackedRegistry(appCtx)
  …  
}

https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/handling/Chain.java#L449-449
https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/handling/internal/DefaultContext.java#L210-210

Whenever you add to the context registry, it’s implicitly “appended” to the existing registry via http://www.ratpack.io/manual/0.9.7/api/ratpack/registry/Registries.html#join(ratpack.registry.Registry, ratpack.registry.Registry)
Reply | Threaded
Open this post in threaded view
|

Re: Preparation of Spring Boot module for Ratpack

Lari Hotari
Hi Luke,

Thanks for your advice. This makes things a lot easier. I was wondering why I had to go down the rabbit hole to add a registry. :)

The changes for Spring Boot integration are now stripped down to this commit:
https://github.com/lhotari/ratpack/commit/8740f36a993d6a7dfd22eb01cd768b9f4d0ba1d7

The author of SpringBackedRegistry is Dave Syer, it's from
https://github.com/dsyer/spring-boot-ratpack/blob/master/src/main/java/ratpack/spring/internal/SpringBackedRegistry.java

Please review the ratpack-spring-boot module so that I can do the fixes, unit tests and cleanup before I send a PR.

I've also updated the simple GORM demo app to use this:
https://github.com/lhotari/ratpack-gorm-example/blob/07d4f81/src/ratpack/Ratpack.groovy#L10

-Lari




Reply | Threaded
Open this post in threaded view
|

Re: Preparation of Spring Boot module for Ratpack

Luke Daley
Administrator
The registry implementation needs some work, but I’ll need to dig into the Spring APIs to fix it. Let’s get it in to our codebase so I can work on it there.

We should probably also consider adding a main class that Groovy users could use instead of the default one, that couples to Guice. That said, most users are probably still going to want to use Guice because of the modules that Ratpack provides. Perhaps we don’t go down this route.

Anyway, send a PR and lets get the code in. Can you add some tests first though please. We’ll need some unit tests for the registry impl, and some integ tests that use a real context like your gorm example.
Reply | Threaded
Open this post in threaded view
|

Re: Preparation of Spring Boot module for Ratpack

Lari Hotari
Thanks for the review and suggestions. I've now created a PR https://github.com/ratpack/ratpack/pull/433 and we can continue the discussion on the PR in Github.