Skip to content
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

Guice @Inject doesn't create default constructor for MVC routes #3567

Closed
tpoll opened this issue Oct 22, 2024 · 7 comments
Closed

Guice @Inject doesn't create default constructor for MVC routes #3567

tpoll opened this issue Oct 22, 2024 · 7 comments

Comments

@tpoll
Copy link
Contributor

tpoll commented Oct 22, 2024

I'm attempting to upgrade to jooby 3.4.2.

currently, our jooby is set up with following guice-module docs to power the router generation. It's working fine for us on 3.0.5.

When updating, I consistently am getting Router not found: **** . Make sure Jooby annotation processor is configured properly..
I've tried to register the routers via the mvc method like MyRouter.class and MyRouter_.class, but both give me the same error.

Is there a new way to make DI injection for mvc work in the generated code world? It seems like the docs for guice haven't updated, so I assumed the old way would work without any changes.

@jknack
Copy link
Member

jknack commented Oct 22, 2024

Hi

The mvc(class) must be avoided. Try:

mvc(new MyRouter_());

This is preferred way. The mvc(class) requires the service locator pattern and I kept mvc(object) for compatibility reason. You probably are missing something, any of these variants should work too.

Preferred:

mvc(new MyRouter_());

Requires jooby processor to generate META-INF entry:

mvc(new MyRouter());
mvc(MyRouter.class);

@kliushnichenko
Copy link
Contributor

@tpoll which one inject annotation are you using? Is it com.google.inject.Inject or jakarta.inject.Inject?
With jakarta should work

@tpoll
Copy link
Contributor Author

tpoll commented Oct 22, 2024

I'm using com.google.inject.Inject.

@jknack I'm trying to avoid mvc(new MyRouter_()) because otherwise I will have to manually construct all the dependencies I pass in, which sort of defeats the purpose of using Guice/DI it seems.

I have verified locally the the meta/INF file is being generated.

@jknack
Copy link
Member

jknack commented Oct 22, 2024

you need to use jakarta(preferred) or javax. If you do that you will see that the generated router MyRouter_ will have a default constructor bc of the Inject annotation.

But yea you need to switch to jakarta

@tpoll
Copy link
Contributor Author

tpoll commented Oct 22, 2024

ah, okay that makes sense. I didn't realize this would require switching to jakarta.

verified locally that switching to jakarta does work.

@tpoll
Copy link
Contributor Author

tpoll commented Oct 22, 2024

I can close this out then.

I know it's a long shot, but is there any appetite for adding guice inject as a supported annotation for the default constructor?

I can double up @Inject annotations to prevent a full if needed, but that feels a little error prone.

@jknack
Copy link
Member

jknack commented Oct 22, 2024

Can you rename the ticket? Should be simple to support due we actually have jakarta/javax (not sure who many people will find it useful)

@tpoll tpoll changed the title Does DI injection work with the new mvc code generated routers? Guice @Inject doesn't create default constructor for MVC routes Oct 22, 2024
@jknack jknack added this to the 3.4.3 milestone Oct 27, 2024
@jknack jknack closed this as completed in d6e3483 Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants