23 Temmuz 2018 Pazartesi

SpringContext ResourceLoaderAware Arayüzü

Giriş
Şu satırı dahil ederiz.
import org.springframework.context.ResourceLoaderAware;
setResourceLoader metodu
Örnek
Şöyle yaparız.
@Component
public class StartupHousekeeper implements ResourceLoaderAware {

  private ResourceLoader resourceLoader;

  @EventListener(ContextRefreshedEvent.class)
  public void contextRefreshedEvent() {
    try {

      // Read file from src/main/resources folder
      File sourceFile = new File(getClass().getClassLoader()
        .getResource("app_dev.json").getFile());  

      // Read file from src/main/webapp folder
      Resource resource = resourceLoader.getResource("file:webapp/AppConfig.json");
      File file=  resource.getFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
  }

  @Override
  public void setResourceLoader(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
  }

}

Hiç yorum yok:

Yorum Gönder