简体   繁体   中英

Custom values not being read from externalized properties file in Spring Boot

I have a Spring boot application whose application.properties looks like what's given below: -

server.port=5000
spring.datasource.url = jdbc:mysql:.......
spring.datasource.username = .......
spring.datasource.password = ......
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR
logging.path=/var/log/


controller.condition.otp = true
.........
.........

I also have a controller called OTP that is enabled or disabled based on the value of the custom variable controller.condition.otp in the config file and on the controller class I have the following condition given

@ConditionalOnProperty(prefix = "controller", name = "condition.otp", havingValue = "true")
@RestController
public class OTP {
........
........
}

When the file (application.properties) resides in the resource folder, the custom config variable is read and the controller works just fine. However, when the same file is moved to a different location and read from there, the custom config variable is ignored / not read and I get a 404 when I hit the controller's path ( /otp/generate ).

Following is how I'm reading the config file from a different location

@Configuration
@PropertySource("file:/.local/application.properties")
public class PropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

You need to provide externalized property location in spring.config.location environment property.

@PropertySource will not work in this case. Issue is logged in Jira https://jira.spring.io/browse/SPR-8539

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM