zoukankan      html  css  js  c++  java
  • [Spring boot] Read values from external properties file

    Let's say we have a extral app.proporites file which contains some extra configuration:

    // resources/app.properties
    external.url="http://somedomain.com"

    We can read the extra propoties by using @Value("${xxx}")

    package com.example.in28minutes.properties;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class SomeExternalService {
    
        @Value("${external.url}")
        private String url;
    
        public String returnServiceURL () {
            return url;
        }
    }

    As you can see, we didn't define where should we looking for "app.proporties" file, this is what we should do in main file by @PropertySource("")

    package com.example.in28minutes;
    
    import com.example.in28minutes.properties.SomeExternalService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.PropertySource;
    
    @SpringBootApplication
    @PropertySource("app.properties")
    public class In28minutesPropotiesApplication {
    
        private static Logger LOGGER = LoggerFactory.getLogger(In28minutesPropotiesApplication.class);
    
        public static void main(String[] args) {
            // Application Context
            ApplicationContext applicationContext =
                    SpringApplication.run(In28minutesPropotiesApplication.class, args);
            SomeExternalService someService = applicationContext.getBean(SomeExternalService.class);
    
            LOGGER.info("{}", someService.returnServiceURL());
        }
    }
  • 相关阅读:
    SQL优化值-exists
    Oracle执行计划详细讲解
    SpringBoot配置加解密工具之Jasypt
    升级Spring Boot 2.0后RelaxedPropertyResolver不可用的解决方案
    Spring, SpringBoot之占位符源码解析
    springboot启动原理深度解析
    Java SPI技术深度解读
    [VCSA7]添加共享磁盘
    [VCSA7]MacOS部署VCSA7
    [Oracle]查看数据库磁盘空间
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10698096.html
Copyright © 2011-2022 走看看