Guice with empty Module
I'm learning Guice and I have working code, but my Module (extending AbstractModule) is empty (because I'm injecting a class not an interface). I can see that there's no overload of Guice.createInjector() which takes no params. Is there some way of not needing a module in my scenario? Not sure if:
1) it's not a big deal; you might add bindings to it later
2) using Guice with an empty module is an anti-pattern - always use interfaces (but I don't think that's correct)
3) I'm missing something.
18 Replies
⌛
This post has been reserved for your question.
Hey @Dave12345! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
What do you mean with the module being empty?
How do you see that?
There's no bindings defined in it.
Can you show a minimal reproducer or similar?
Can you show the class you are injecting and its implementation?
Can you show the module?
Can you show whatever shows you the (empty) bindings?
Below is the relevant code. To be clear, this is a "best practice" type question. Like I said up front, this works. There's nothing that shows me "empty bindings". I simply have a (what looks to me at least) pointless empty module and I'm wondering if there's a way to do away with it.
so the code/body is the thing that's empty
Yes. "my Module (extending AbstractModule) is empty". I'm probably phrasing it wrongly?
I think this should be perfectly fine but you might be able to use an anonymous class
Usually it's not empty as you need to bind an implementation to an interface
ok, i'll give that a go - thanks!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
yeah I understand what you mean with that
However, I would personally prefer an empty module class
that's my instinctual preference too. I'm using java at lot at work and it's a huge project with every technology in the world in it. I'm spending a little time at home using some of the tech but in a much smaller test project and trying to learn how to do stuff very cleanly and properly. At work there's some crazy injector class which is populated from multiple modules and service loaders and god knows where else so you can inject anything anywhere so you never need to know which module is used etc. So I've never had to actually create a module or write a .createInjector etc - it's already done somewhere!
yeah if your application is sufficiently big, you'd probably have something you need to declare mappings for anyway
ok thanks for your help!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@1187874496602984668>.