Play 2.5.3: Using dependency injection to get configuration values

前端 未结 5 1948
忘了有多久
忘了有多久 2021-02-05 22:59

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 t

5条回答
  •  心在旅途
    2021-02-05 23:26

    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.

提交回复
热议问题