如何更改外部 application.properties 文件名?

How to change external application.properties file name?

我正在使用 spring 引导并将其作为 war 独立部署 tomcat.The 下面是我的应用程序 class。

public class APIApplication extends SpringBootServletInitializer  {
    public static void main(String[] args) {

        configureApplication(new SpringApplicationBuilder()).run(args);
    }

    public static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
        return builder.sources(APIApplication .class).properties(getProperties());
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(APIApplication .class);
    }

    public static Properties getProperties() {
        Properties props = new Properties();
         props.setProperty("spring.config.location",
         "/home/config_directory/");
        props.setProperty("spring.config.name", "apiapplication");
        return props;

    }


}

但这不起作用,也无法从 /home/config_directory/apiapplication.properties

中读取

感谢任何帮助。

编辑

也试过

public static void main(String[] args) {
        System.setProperty("spring.config.location","/home/config_directory/");
        System.setProperty("spring.config.name", "apiapplication.properties");

        SpringApplication.run(DriverguidanceapiApplication.class, args);
        //configureApplication(new SpringApplicationBuilder()).run(args);
    }

也没用。

我建议您使用@PropertySource 注释并自动装配环境对象。检查示例:

    @PropertySource("file:/home/config_directory/")
    public class testClass{
        @Autowired
        private Environment environment;
        public DataSource dataSource(){

          BasicDataSource basicDataSource = new BasicDataSource();   
          basicDataSource.setDriverClassName(environment
                             .getProperty("database_manager.db.driver"));
          return basicDataSource;
      }
    }

哥们不用手动配置!! Spring 可以帮助您 @PropertySource 注释。

我将分享我的代码片段,其中我使用了您正在寻找的内容。

package XXXX;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**
 * Created by Pratik Ambani.
 */
@Configuration
@PropertySource(value = {"classpath:default.properties", "classpath:application.properties"}, ignoreResourceNotFound = true, name = "myServerConfigs")
public class PropertySourceExample {

    @Value("${dev.baseURL}")
    private String localUrl;

    @Value("${sit.baseURL}")
    private String serverUrl;

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

    @Bean
    protected String database() {
        Resource resource = new Resource();
        resource.setUrl(restAPIUrl);
        return resource;
    }
}

等等,这些 dev.baseUrl 和 sit.baseUrl 值是什么?它们来自哪里?看看我的属性文件。

application.properties

dev.baseURL=http://localhost:7012

default.properties

sit.baseURL=http://myserver:7012

瞧! 我能够从多个文件中读取值。 快乐编码。愿代码保佑你。 :)

by Daniel Mora. Daniel gave a good solution but if you want to use spring.config.name property see my answare:

中所述,使用 @PropertySource 并不是最佳选择