Shiny
Shiny

Reputation: 191

how to tell spring to load resources from external folder?

I'm trying to read a text file in config folder - MyProject/config in spring boot configuration class using MyConfig.class.getResourceAsStream(/config/test.txt).

MyProject
  -config
    -test.txt
  -src
    -main
      -java
        -com
          -myproject
            -configurations
              -MyConfig

It always return null as the resource not found. Whereas it can read the file if it is located under MyProject/src/main/resources folder.

MyProject
  -src
    -main
      -resources
        -test.txt
      -java
        -com
          -myproject
            -configurations
                -MyConfig

Is there any specific configuration required to tell spring-boot to check the entire path of the project?

@Configuration
@EnableAutoConfiguration
public class MyConfig {

  @Bean
  public Config config() { 
    return new Config(MyConfig.class.getResourceAsStream("/config/test.txt"); // This doesn't work. The same setup work if I have the file under resources folder.MyConfig.class.getResourceAsStream("/test.txt")
  }
  }

Upvotes: 0

Views: 1996

Answers (3)

Nyamkhuu Buyanjargal
Nyamkhuu Buyanjargal

Reputation: 649

For example<> E:\MyProject\config\test.txt

Upvotes: 0

FranAB
FranAB

Reputation: 96

Assuming you have a typicall project structure, try to put test.txt file in src/main/resources folder in order to be scanned by springboot context.

Upvotes: 0

Vasco
Vasco

Reputation: 802

That method will load only from classpath/modulepath. Check of the config folder is in the classpath.

Upvotes: 0

Related Questions