Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.5.3: Using dependency injection to get configuration values

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")

like image 427
Miguel Avatar asked May 01 '16 21:05

Miguel


People also ask

How do you perform a dependency injection?

The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

Does dependency injection affect performance?

There is a performance impact, but its effect will depend on many different variables. Every architectural decision involves research and weighing pros and cons, and ultimately boils down to a subjective opinion.

What is @inject in Java?

javax.inject. Annotation Type Inject. @Target(value={METHOD,CONSTRUCTOR,FIELD}) @Retention(value=RUNTIME) @Documented public @interface Inject. Identifies injectable constructors, methods, and fields. May apply to static as well as instance members.

What is dependency injection pick one option?

Q 3 - What is Dependency Injection? A - It is a design pattern which implements Inversion of Control for software applications.


2 Answers

Try with constructor injection instead:

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

public class Zipper {

    private Configuration configuration;

    @Inject
    public Zipper(Configuration config) {
        this.configuration = config;
    }

    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");
    }
}

I'm not sure that Guice is capable of inject private fields. Anyway, constructor injection is the recommended injection type.

like image 160
marcospereira Avatar answered Nov 02 '22 01:11

marcospereira


I think that you can initialize the configuration like this:

private  Configuration configuration = Play.current().injector().instanceOf(Configuration .class);

So, your Zipper will be:

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

public class Zipper {

    private  Configuration configuration = Play.current().injector().instanceOf(Configuration .class);

    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");
    }
}
like image 6
asch Avatar answered Nov 02 '22 01:11

asch