问题
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:
- Define import: import
javax.inject.*; import play.Configuration;
- Define class property:
@Inject private Configuration configuration;
- 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")
回答1:
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");
}
}
回答2:
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.
回答3:
I put here the answer, in order to help anyone with the same issue
My error came from the way i used to instantiate my Zipper java class from my calling class.
Thx to Igmar Palsenberg, he provided me the answer : https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/play-framework/uLFqTM9_Iy4
I used Zipper zipTest = new Zipper(); to instanciate my Zipper class and i have to use Zipper zipTest = injector.instanceOf(Zipper.class);
回答4:
you should try to remove private
.
use:
@Inject Configuration configuration;
instead of:
@Inject private Configuration configuration;
回答5:
Try to annotate your class with Singleton so that play can detect your bean to inject your resources.
来源:https://stackoverflow.com/questions/36972305/play-2-5-3-using-dependency-injection-to-get-configuration-values