20

I have a simple main app:

@Configuration
    @EnableAutoConfiguration
    @ComponentScan(basePackages = "dreamteam.eho")
    @Import({EhoConfig.class})
    public class MainApp implements CommandLineRunner, ApplicationContextAware {

With config:

@Configuration
@EnableConfigurationProperties({RootProperties.class})
public class EhoConfig {
}

And properties:

@ConfigurationProperties("root")
public class RootProperties {
    private String name;

I try to load config:

--spring.config.location=file:///E:/.../eho-bot/props/ --spring.profiles.active=eho

Path is correct. But yml isn't loaded;

application-eho.yml file:

logging:
  file: D:/java/projects/telegram-bots/eho.log
  level:
    dreamteam.eho: INFO
    org.springframework: DEBUG

root:
  name: EHO-BOT

App runs with args, but all props null. Logging properties not aplied; sout:

--spring.config.location=file:///E:.../eho-bot/props/

--spring.profiles.active=eho

--spring.output.ansi.enabled=always
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
yazabara
  • 1,253
  • 4
  • 21
  • 39
  • You haven't provided enough info. For example, what does the rest of your main class look like? Have you passed its args into the SpringApplication that it runs? – Andy Wilkinson Apr 21 '16 at 07:07
  • `@EnableConfigurationProperties` is not required in my opinion. What exception are you getting? – Aritz Apr 21 '16 at 07:07
  • args from app: ```--spring.config.location=file:///E:/Dropbox/Programming/java/telegram-bots-app/eho-bot/props/``` ```--spring.profiles.active=eho``` ```--spring.output.ansi.enabled=always``` – yazabara Apr 21 '16 at 07:14
  • That doesn't help. We need to see all of the code in `MainApp`, most importantly its `main(String[] args)` method. – Andy Wilkinson Apr 21 '16 at 08:51
  • -Dspring.profiles.active=test <- I normally pass it as jvm parameter and it works – Palcente Apr 21 '16 at 09:33
  • We had a similar issue and were able to resolve using this: Added @PropertySource("file:C:/Users/apps/config/application.yml") at the class declaration level for Spring Boot's Application.java. Another thing we found out that if we changed the name of application.yml to anything else like application2.yml, it doesn't get picked up. May be you can try that. Please let us know. – JavaTec Jul 27 '16 at 20:37

3 Answers3

1

Try this way :

Follow application structure like :

App
└── src
|    ├── main
|         ├── java
|         │     └── <base-package>
|         │               └── Application.java (having public static void main() method)
|         │
|         ├── resources
|                ├─── application-eho.yml
|
├──── pom.xml

Application.java content

@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "application-eho");
        SpringApplication.run(Application.class, args);
    }

}

application-eho.yml file:

logging:
  file: D:/java/projects/telegram-bots/eho.log
  level:
    dreamteam.eho: INFO
    org.springframework: DEBUG

root:
  name: EHO-BOT
Riddhi Gohil
  • 1,758
  • 17
  • 17
  • 2
    This won't help. The OP is trying to use a profile-specific configuration file. Boot looks for `application-eho.properties` and `application-eho.yml` because the `eho` profile is active. Setting the name as you have would mean that it looks for `application-eho-eho.properties` and `application-eho-eho.yml`. – Andy Wilkinson Apr 21 '16 at 08:53
  • Did u make that "eho profile is active." ?? Simple suggesion is dont do any other configurations.. Just do as I did in Application.java class. Its working for me .. In application like "microservices". – Riddhi Gohil Apr 21 '16 at 08:57
  • I have application.yml in that folder but still doesnt load eureka url – Philip Rego Mar 09 '22 at 02:04
1

For this moment you should use spring-boot.

    @SpringBootApplication
    public class ReTestsApplication implements CommandLineRunner {

        public static void main(String[] args) {
            SpringApplication application = new SpringApplication(ReTestsApplication.class);
            application.setWebEnvironment(false);
            application.setBannerMode(Banner.Mode.OFF);
            application.run(args);
        }

        public void run(String... args) throws Exception {

        }
    }

Use webEnvironmet=false, and BannerMode=off (console application).

Docs, see https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-externalize-configuration

yazabara
  • 1,253
  • 4
  • 21
  • 39
0

if you are using PropertySource annotation then it is not possible. Be default it doesn't support yml file types. use PropertySourceFactory as below

    public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) 
      throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }
}

Then, refer to the same in your property source annotation as follows:

@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class)

Source: PropertySourceFactory

Karthikeyan
  • 2,634
  • 5
  • 30
  • 51