What is the alternative to Play.application ()

I am new to the Play platform. I wanted to read the file that is in the conf folder. So I used.

Play.application().classloader().getResources("Data.json").nextElement().getFile()

But I found out that the game. The payment is now out of date. What can I use to read a file. I read this article and cannot understand what it says.

+2
source share
4 answers

Just enter the application into the class in which you need it. Suppose it is in the controller:

import play.Application;
import javax.inject.Inject;
import javax.inject.Provider;

class YourController extends Controller {

    @Inject
    Provider<Application> app;


    public Result someMethod() {
        // (...)
        // File is placed in conf/Data.json
        InputStrem is = app.get().classloader().getResourceAsStream("Data.json");
        String json = new BufferedReader(new InputStreamReader(is))
                .lines().collect(Collectors.joining("\n")); 
        return ok(json).as("application/json");    
    }
}
+3
source

2.5, , Play Injection Dependency - , . , .

( application.conf, , .conf: -

my_conf_key = "some value"

2.5: -

import play.api._
import play.api.mvc._
import javax.inject.Inject

class TestConf @Inject() (conf: Configuration) extends Controller {

  def config = Action {
    Ok(conf.underlying.getString("my_conf_key"))
  }

}

: -

some value
+1

. Play.

Play 2.5.

, .

+1

Application , Environment, environment.resource("resource.xsd");

:

import javax.inject.Inject;

public class ExampleResource extends Controller{

     private final Environment environment;

     @Inject
     public ExampleResource(Environment environment){
          this.environment = environment;
     }

     public void readResourceAsStream() {
          InputStream resource = environment.resourceAsStream("data.xsd");
          // Do what you want with the stream here
     }

     public void readResource(){
          URL resource = environment.resource("data.xsd");
     }
}

Reproduction of documentation about the application interface: https://www.playframework.com/documentation/2.6.9/api/java/play/Application.html

0
source

Source: https://habr.com/ru/post/1653206/


All Articles