Giter VIP home page Giter VIP logo

guice-property's Introduction

guice-property

Injecting environment variables and properties with Guice

Why?

Using environment variables is a must for any meaningful project. Using Guice, we can actually directly inject the values of the variables into our classes, without any clumsy Configuration class and alike.

This project does just that.

How?

  1. Add your variables into the Property enum, as many as you want:

    SERVER("MY_SERVER", "my.server", "localhost", "The server"),
    PORT("", "my.port", "1234", "The port");

    Basically this says the Property.SERVER property will be filled using:

    • First, the value of the MY_SERVER environment variable, if it exists,
    • second, the my.server property (passed with the -D flag), if it exists,
    • and finally, the default value localhost if none of the above is available.

    Feel free to customize this logic in Property.getValue().

  2. Directly inject each variable into your class using the @Prop annotation:

    import com.google.inject.Inject
    import phvu.prop.{Prop, Property}
        
    class PropertyUser @Inject()
    (@Prop(Property.SERVER) val server: String, @Prop(Property.PORT) val port: Int) {
    
      def run() = {
        println(s"I am on $server:$port")
      }
    }

    This is written in Scala, but using it in Java is very similar.

  3. Use Guice to create the injector:

    Guice.createInjector(new PropertyModule).getInstance(classOf[PropertyUser]).run()

    Don't worry about binding. Those are handled for you.

How to test?

$ sbt clean compile package assembly
$ MY_SERVER="fooooooooo" java -Dmy.server="mystupidserver" -Dmy.port=342456456 -jar target/scala-2.11/guice-properties-assembly-1.0.jar

I am on fooooooooo:342456456

FAQ

  1. How to use this in my project?

    It is up to you. You can copy the classes and freely modify it. However, I would recommend to make this into a separated module, as it will be easier if you need to shade some dependencies (notably Guice and Guava).

  2. What if I have some other classes to be injected with Guice?

    Be aware that you can create an Injector from multiple Module:

    val injector = Guice.createInjector(new PropertyModule, new YourModule...)

References

Inspired by this, I wanted to re-make the whole project in Scala, but there are some limitation in the way Scala handle annotations, so I had to resorted back to Java.

I also used bindConstant() instead of bind() to have Guice automatically do type-casting.

guice-property's People

Contributors

phoaivu avatar phvu avatar

Stargazers

Denilson Anachury avatar Kevin Gann avatar Matt Menzenski avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.