A guice module discovery module with support for multiple environments. Alchemy Inject tries to deal with the problem of creating guice injector in a reliable and decentralized manner. The idea is to write a guice module and mark it with an environment it is relevant it and it should be applied.
The injector also contains a discovery mechanism for discovering Jackson modules and auto injecting the ObjectMapper.
compile 'com.strandls.alchemy:alchemy-inject:0.9'
<dependency>
<groupId>com.strandls.alchemy</groupId>
<artifactId>alchemy-inject</artifactId>
<version>0.9</version>
</dependency>
You use Alchemy Inject by annotating your guice modules with the AlchemyModule annotation like so
import com.google.inject.AbstractModule;
import com.strandls.alchemy.inject.AlchemyModule;
import com.strandls.alchemy.inject.AlchemyModule.Environment;
/**
* Bindings for authentication.
*
* @author ashish
*
*/
@AlchemyModule(Environment.All)
public class AuthModule extends AbstractModule {
.
.
.
A module is be annotated with an environment it is to be used in. Three values are supported today.
- Prod - this module should be used in production settings
- Test - this module should be used in test settings
- All - this module should be used across both test and production settings
Create a guice injector using all production modules, including modules annotated with environment All as well, like so
import com.google.inject.Injector;
import com.strandls.alchemy.inject.AlchemyModule.Environment;
import com.strandls.alchemy.inject.AlchemyModuleLister;
Injector injector = Guice.createInjector(new AlchemyModuleLister().getModules(Environment.Prod))
Note you can now keep adding newer guice modules without having to worry about changing the injector creatiion code.
With large complex projects you might hit a case where you have conflicting bindings. With Alchemy inject you could resolve these bindings or filter out some modules using a configuration file placed in your application classpath.
The file, written in .ini format, should be named alchemy-modules.ini. Here is a sample file
[Prod]
filter=(?i).*dummy.*
filter=com.strandls.alchemy.webservices.auth.AuthModule
[Test]
filter=com.strandls.alchemy.webservices.client.StaticCredentialsModule
filter=com.strandls.alchemy.webservices.client.JaxRsClientModule
With this configuration file all modules with dummy (ignoring case) in there fully qualified class name, will be filtered out from the production environment. The filter expressions are [JavaRegex][Java regular expressions].
The Alchemy Rest Client Demo project is a good demostration of real life use of this module.
Please refer to Contribution Guidlines if you are not familiar with contributing to open source projects.
The gist for making a contibution is
- Fork
- Create a topic branch -
git checkout -b <your branch>
- Make your changes
- Push to your branch -
git push origin <your branch>
- Create an [Issue] with a link to your branch
Run
gradle/gradlew eclipse
Import alchemy inject to eclipse using File > Import > Existing Projects into Workspace
The project has been setup to auto format the code via eclipse save actions. Please try not to disturb this.
Code and documentation copyright 2015 Strand Life Sciences. Code released under the Apache License 2.0. Docs released under Creative Commons.