Java – master Google Guice

I just started using Google Guice as a dependency injection framework and tried to transform it into a small and medium-sized project I recently wrote I know the basics of how Guice works, but I'm a little vague about the details of some methods For example:

1) The module is used to define the binding and then bind it to the injector Do you tend to put everything in one module, or do you tend to break things down into many smaller modules?

2) Do you inject a syringe that injects the entire object tree or multiple syringes at the top level, only the dependencies you really need to inject? I consider my own code base here. Of course, it has many dependencies, but only a few I need to control during testing

3) I stay a little bit on the best way to test the system / integration using only the modules of the test environment rather than the production version This problem is likely to be a concrete implementation, but I'm curious about what methods people use As a reference, my application is a servlet - based web application

Any other pointers?

Solution

1) Usually you break things down into multiple modules One of Guice's goals is to help code modularization, which is module How you break all this is up to you (obviously you absolutely don't need it) One advantage of more elaborate modules is that you can define modules in a specific package and make the class package - private Since the module is in a package, these concrete classes can be bound and used to configure the injector (in another package) In addition, you can make your code more flexible by changing one module to another without having to change the code in a single module

2) Yes, a top syringe that injects an entire object tree is usually the right thing to do This goes back to modules... Use them to break down dependencies into groups and use an injector

3) Use different entry point classes to configure the injector For a stand-alone application, I will have a different main class... For webapp, I think you can test it with guiceservletcontextlistener alone You can then replace the entire module with a module for testing, or use modules Override replaces the binding in a specific module, etc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>