Accessing the application.conf properties from java class with Play! 2.0

前端 未结 13 2062
一整个雨季
一整个雨季 2020-12-24 10:37

I want to add an object to the Global scope, and in order to construct it I need to pass it a path to a file. I don\'t want to hard code the file path in the source, and so

相关标签:
13条回答
  • 2020-12-24 11:02

    import play.Play; String myVal = Play.configuration.getProperty("your.key").toString();

    i use this in my app and it works

    Dont forget to import play.Play. Hope it'll gives you help

    0 讨论(0)
  • 2020-12-24 11:03

    Since Play 2 uses the Typesafe config library, I accessed my vars in application.conf like this :

    ConfigFactory.load().getString("my.var");
    
    0 讨论(0)
  • 2020-12-24 11:06

    Use as following (Tested in Play 1.2.5)

    ${play.configuration.getProperty('my.var')}
    

    where my.var should be specified in application.conf file

    0 讨论(0)
  • 2020-12-24 11:06

    For Java Playframework:

    In Application.conf, you can put something like that:

    email="example@gmail.com.pe"

    some class:

    import play.Play;

    String email = Play.application().configuration().getString("key") // key ->email

    0 讨论(0)
  • 2020-12-24 11:11

    As a reference to access it from the template (for play < 2)

    play.configuration['your.key']
    
    0 讨论(0)
  • 2020-12-24 11:12

    From Play 2.4 and + it is better to use dependency injection to access Configurations:

    import play.Configuration;
    import javax.inject.Inject;
    
    
    @Inject
    private Configuration configuration;
    
    ...
    
    String value = configuration.getString("your.key");
    
    0 讨论(0)
提交回复
热议问题