[java play 2.5.x] how to read from application.conf

5,986 views
Skip to first unread message

Mohamed Madian

unread,
Apr 6, 2016, 8:57:11 AM4/6/16
to play-framework
Dears,
I'm try to read configuration from application.conf in play 2.5.x but return nulls, kindly provide be with the best practice in read from application.conf
Thanks.

Igmar Palsenberg

unread,
Apr 6, 2016, 2:55:52 PM4/6/16
to play-framework
 
Dears,
I'm try to read configuration from application.conf in play 2.5.x but return nulls, kindly provide be with the best practice in read from application.conf
Thanks.

That is normally handled by play.Configuration. What exactly is your question ?


Igmar 

Faiyaz Md Abdul

unread,
Apr 6, 2016, 2:58:07 PM4/6/16
to play-framework
Play.application().configuration().getString("your_key") ;


Igmar Palsenberg

unread,
Apr 6, 2016, 3:00:45 PM4/6/16
to play-framework
The usage of Play.* is deprecated, and won't properly in a number of cases. The right way is to @Inject the Configuration class, and use that. Keys that don't exist will return null however, unless you specify a default.



Igmar 

Mohamed Madian

unread,
Apr 7, 2016, 8:11:57 AM4/7/16
to play-framework
im just setting, my configuration to read them at run time like:
this added at application.conf
daf {
"ConnectionDriver"="XXXXXX"
"ConnectionURL"="XXXXXXX"
"Salt"="XXXXXX"
}
i tried to inject configuration and read it but null returned
@Inject private static Configuration configuration;

...

String temp = configuration.getString("SqlConnectionURL"); //return null

Igmar Palsenberg

unread,
Apr 7, 2016, 8:17:50 AM4/7/16
to play-framework


Op donderdag 7 april 2016 14:11:57 UTC+2 schreef Mohamed Madian:
im just setting, my configuration to read them at run time like:
this added at application.conf
daf {
"ConnectionDriver"="XXXXXX"
"ConnectionURL"="XXXXXXX"
"Salt"="XXXXXX"
}
i tried to inject configuration and read it but null returned
@Inject private static Configuration configuration;

...

String temp = configuration.getString("SqlConnectionURL"); //return null


Guice won't inject static fields.


Igmar

simin Ghasemi

unread,
May 3, 2016, 12:31:01 AM5/3/16
to play-framework

To read from application.conf in play 2.5, you should inject configuration in your controller. Because Configuration is a concrete class, binding it in Module class is not needed. Below is a sample how to read property "key" from application.conf :

public class HomeController extends Controller {
   
private Configuration configuration;

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

   
public Result index() {
       
String value = configuration.getString("key");
       
System.out.println("value of key is " + key);
       
return ok(value);
   
}

}

see also this discussion:
http://stackoverflow.com/questions/36634019/play-framework-2-5-1-routing-and-dependency-injection-for-java

Thiago Fabre

unread,
Jul 7, 2016, 9:45:40 AM7/7/16
to play-framework
And How can I access application.cont outside a Controller

Greg Methvin

unread,
Jul 7, 2016, 4:21:34 PM7/7/16
to play-framework
Same way you access it within a controller: dependency injection. Just inject Configuration into the constructor of your component.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/75865d73-f607-4be2-9dcb-739db8abd745%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Ala Schneider

unread,
Jul 20, 2016, 3:51:29 AM7/20/16
to play-framework
This link should answer your question

Ala Schneider

unread,
Jul 20, 2016, 3:51:55 AM7/20/16
to play-framework
See this link 

Justin L

unread,
Apr 20, 2017, 5:28:23 PM4/20/17
to Play Framework
Has anyone been able to access configuration outside of a controller without using the deprecated method?

The examples above (and in the link) only work in a controller. If you put it in a non-controller class, configuration will be null. The injection is not happening.

There are a few of us talking about this here: https://gitter.im/playframework/playframework

I'm using Java and Play Framework 2.5.

Ala Schneider

unread,
Apr 20, 2017, 5:55:17 PM4/20/17
to Play Framework
The way to inject configuration from a none-controller class is to call Play.currnt().injector In case of Configuration it is like this:

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

The full explanation you can found in this link.

Justin L

unread,
Apr 20, 2017, 8:51:58 PM4/20/17
to Play Framework
It doesnt seem to work for me.


I get a compile time error:

Error:Play 2 Compiler: 
   reason: actual and formal argument lists differ in length
                             Obj o = new Obj();
 /home/me/project/app/models/Obj.java:107: cannot find symbol

I assume this doesnt work because the injection is not working in this non-controller method.


Also I might be misunderstanding but it seems like the docs are saying Play.current is deprecated?

Justin L

unread,
Apr 20, 2017, 8:54:03 PM4/20/17
to Play Framework

I know in the gist your bit is commented  out but that error I pasted is what I get with the Play.current().... line of code in.

Igmar Palsenberg

unread,
Apr 21, 2017, 3:21:40 AM4/21/17
to Play Framework
 
I know in the gist your bit is commented  out but that error I pasted is what I get with the Play.current().... line of code in.

You need to make sure that the class Configuration is injected in is provided by the DI framework. We usually bind that class in a Guice module, and then inject it somewhere.
In general, if you code contains Play.<something>, it's wrong, and it will stop working in 2.6.


Igmar

Greg Methvin

unread,
Apr 21, 2017, 3:24:17 AM4/21/17
to play-framework
Yes, Play.current is deprecated. The whole point of using DI is to avoid global state like Play.current.

What kind of class are you trying to inject something in? Is it not possible to pass it as a dependency to the controller constructor, or the constructor of some component the controller depends on?

If your controller doesn't depend on the component you're trying to inject, then how is it being used? If it is doing a background task, for example, you might need to bind as an eager singleton: https://www.playframework.com/documentation/2.5.x/JavaDependencyInjection#Eager-bindings

If you are trying to inject into a data type (e.g. a model like a Person object), you can use AssistedInject to create a factory. But typically it is considered bad practice to have data objects that access state. Ideally you would have (for example) a Person that is self-contained, and a PersonRepository that does database queries, and is injected into your controller or other component.

Also there are a number of up-to-date examples on the playframework github that show how to design basic apps with dependency injection. See for example: https://github.com/playframework/play-java-jpa-example/

Greg

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/23ef39eb-9034-486a-92c4-04ae9a77f8a7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Will Sargent

unread,
Apr 21, 2017, 3:24:24 AM4/21/17
to play-fr...@googlegroups.com
You can inject it:

public class MyClass {

   @Inject
   public MyClass(Configuration configuration) {

   } 

}

etc

--
Will Sargent
Engineer, Lightbend, Inc.


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.

Ala Schneider

unread,
Apr 21, 2017, 7:28:30 AM4/21/17
to Play Framework
In spite Play is deprecated it still may be used. Your compilation error is not related to "deprecated" injector usage. I guarantee to you that this is is working  in runtime, not only passes the compilation. 
But the better and recommended way is to use injection e2e. Adding @Inject annotation to the class will do nothing, if the instance is not created in the class, injected by Play under the veil. The class should also be added for injection binding into the Module. Its instance should not be created with new, but with injection annotation. It means, that the upper class, which is using injected data member should be also registered for injection. So you should engineer properly the whole injection tree. 
In the bottom line, you can start with deprecated way (and find, what is the reason of the compilation error) and migrate to the DI way further.

Greg Methvin

unread,
Apr 22, 2017, 12:39:29 PM4/22/17
to play-framework
On Fri, Apr 21, 2017 at 4:28 AM, Ala Schneider <ala.sc...@gmail.com> wrote:
In spite Play is deprecated it still may be used. Your compilation error is not related to "deprecated" injector usage. I guarantee to you that this is is working  in runtime, not only passes the compilation. 

The compilation error is not related. I just don't agree with the recommendation to use Play.current. Let's better understand what he's trying to do before we recommend something.
 
But the better and recommended way is to use injection e2e. Adding @Inject annotation to the class will do nothing, if the instance is not created in the class, injected by Play under the veil. The class should also be added for injection binding into the Module. Its instance should not be created with new, but with injection annotation. It means, that the upper class, which is using injected data member should be also registered for injection. So you should engineer properly the whole injection tree. 
In the bottom line, you can start with deprecated way (and find, what is the reason of the compilation error) and migrate to the DI way further.

If you're starting a new app you should avoid static state from the beginning. If you're migrating an existing app that uses statics, you can use static injection, which is better because it doesn't introduce a dependency on the entire application.
 

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

RPGamer

unread,
May 8, 2017, 5:33:46 AM5/8/17
to Play Framework
How do you inject in a static method, like a helper ?
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Greg Methvin

unread,
May 8, 2017, 5:50:15 AM5/8/17
to play-framework
Not sure what you're asking about. I linked the Injections page of the Guice docs, which has a section on static injection: https://github.com/google/guice/wiki/Injections.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/b6859322-3407-4168-8e9d-d3bb530b1e6e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Igmar Palsenberg

unread,
May 8, 2017, 7:03:20 AM5/8/17
to Play Framework
 
How do you inject in a static method, like a helper ?

Make the object the helpers are located in a Singleton object, and inject Configuration in there : 

final class FooBar { 

private Configuration configuration;

public FooBar(final Configuration configuration) {
    this.configuration = configuration;
}

public Boolean helper1() {
        final Boolean alive = configuration.getBoolean("some-value, false);
        ....
}
}

In a module : 

bind(FooBar.class).in(Singleton.class);

And then just @Inject FooBar where you want to use it.



Igmar
Reply all
Reply to author
Forward
0 new messages