[play 2.5.3][Using dependency injection to get configuration values]

1,763 views
Skip to first unread message

miguel...@gmail.com

unread,
May 2, 2016, 6:14:28 AM5/2/16
to play-framework

I'm trying to migrate a Playframework application from 2.4 to 2.5.3 and I have problems to get values from application.conf file:


Before to get a value of from application.conf what I do was:

Play.application().configuration().getString("label")

Now as Play.application() is deprecated, I should use Dependency injection. Based on the framework documentation I use the following instructions:

  1. Define import: import javax.inject.*; import play.Configuration;
  2. Define class property@Inject private Configuration configuration;
  3. Use the configuration class property on my class

When I follow these instructions on my controller Application.java it is working perfectly.


But when I try to use it on an other class object from my project, the dependency injection is not working and I always get a NullPointerException.

Can someone give me an example about how to get values from application.conf using dependency Injection?



Some part of my java code where I try to use the DI:

import javax.inject.Inject;
import play.Configuration;
import play.Logger;

public class Zipper {

    @Inject private  Configuration configuration;

    public void unZip(String zipFilePath) {
        Logger.debug("Display : zipFilePath"+zipFilePath);
        Logger.debug("before call parameter from application.conf");
        Logger.debug("configuration.getString = "+configuration.getString("Unzipedfile.path"));
        Logger.debug("aftercall parameter from application.conf");
    }
}

And I always get a null pointer exception, at the line with configuration.getString("Unzipedfile.path")


FYI: i call the class Zipper from an other java class like this : Zipper zipTest = new Zipper();


Is it a limitation, meaning only my controller "application.java" is able to inject correctly the Configuration class ?


Igmar Palsenberg

unread,
May 2, 2016, 8:46:52 AM5/2/16
to play-framework

But when I try to use it on an other class object from my project, the dependency injection is not working and I always get a NullPointerException.

Can someone give me an example about how to get values from application.conf using dependency Injection?

 
public class Zipper {


    @Inject private  Configuration configuration;

    public void unZip(String zipFilePath) {
        Logger.debug("Display : zipFilePath"+zipFilePath);
        Logger.debug("before call parameter from application.conf");
        Logger.debug("configuration.getString = "+configuration.getString("Unzipedfile.path"));
        Logger.debug("aftercall parameter from application.conf");
    }
}

And I always get a null pointer exception, at the line with configuration.getString("Unzipedfile.path")


FYI: i call the class Zipper from an other java class like this : Zipper zipTest = new Zipper();


This bypasses dependency injection. You have a few options : 

 public class Zipper {
    @Inject
    public  Zipper(Configuration configuration) {
    }
}

@Inject Injector somewhere, and do :

Zipper zipper = injector.instanceOf(Zipper.class);

Or, in a module, bind it (to a singleton) (assuming is is suitable as such)

public class GlobalBootstrapModule extends AbstractModule {
     @Override
      protected void configure() {
            bind(Zipper.class).in(Singleton.class);
      }
}

or just bind it if it's not suppose to be a Singleton

Or better, use a Guice module, and give a specialised instance back (using a provider).



Igmar 

Simon Jacobs

unread,
May 2, 2016, 8:57:28 AM5/2/16
to play-framework

The NPE occurs because the Zipper class is not instantiated by the DI Framework.

Add @Singleton to your Zipper class and inject Zipper to e.q. a controller to make sure Guice will instantiate the class for you and inject it's dependencies.

Regards

miguel...@gmail.com

unread,
May 2, 2016, 5:01:50 PM5/2/16
to play-framework
It works, thx a lot :) 
Reply all
Reply to author
Forward
0 new messages