【问题标题】:How to read external properties file In Spring?如何在 Spring 中读取外部属性文件?
【发布时间】:2019-07-31 02:22:33
【问题描述】:

大家都知道如果要读取properties文件,可以这样做:

@Configuration
@PropertySource("classpath:/application.properties")
public class AppConfig {

    @Value("${app.name}")
    public String name;


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

    @Bean
    public PostService postService() {
        return new PostServiceImpl(name);
    }

}

但是,现在我有一个类似于 SpringBoot 的框架。它可以将Spring与Mybatis集成。

问题是前面的代码只能读取我的项目类路径文件,但我需要使用我的框架读取属性文件项目。我该怎么做?

更新

对不起大家。可能我没说清楚,所以上图:

  • 我不使用 SpringBoot

  • 我想读取项目(使用我的框架)类路径,而不是我的框架类路径。

谢谢。

【问题讨论】:

  • 出了什么问题?
  • 这是您想要完成的任务吗? stackoverflow.com/questions/29072628/…
  • @Deadpool 问题是前面的代码只能读取我的项目类路径文件,但我需要使用我的框架读取属性文件项目。我是怎么做到的?
  • @neildo 谢谢。但不是因为我不使用 SpringBoot。
  • 如果您要读取的属性文件(无论它来自哪个框架)位于类路径上,您的代码应该可以工作。如果它不在类路径上,您也可以将@PropertySource 与文件位置一起使用:@PropertySource("file:/application.properties")

标签: java spring


【解决方案1】:

Spring 框架可以从不同位置读取外部配置文件。 它可以从您的项目目录中读取配置文件,但您需要删除此行:

@PropertySource("classpath:/application.properties")

这将其限制在您的应用程序类路径中。 您可以查看here 以查看 spring 读取配置文件的不同位置。

【讨论】:

  • 也许你误解了我的问题。你能看看我的问题再试一次吗?我编辑了它
【解决方案2】:

Spring 提供外部配置。这样您就可以在不同的环境中运行您的应用程序。

参考链接: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

如果不喜欢 application.properties 作为配置文件名,可以通过指定 spring.config.name 环境属性来切换到其他文件名。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:db.properties")
@PropertySource("classpath:project.properties")
public class DBConfiguration {

@Autowired
Environment env;

  @Bean
  public DBConnection getDBConnection() {
    System.out.println("Getting DBConnection Bean for 
    App:"+env.getProperty("APP_NAME"));
    DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"), 
     env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"), 
     env.getProperty("DB_PASSWORD").toCharArray());
    return dbConnection;
  }

 }

DB.properties:
#Database configuration
DB_DRIVER_CLASS=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/Test
DB_USERNAME=root
DB_PASSWORD=root

project.properties:
APP_NAME=TEST APP

【讨论】:

  • 也许你误解了我的问题。你能看看我的问题再试一次吗?
【解决方案3】:

如果你只是想自己从类路径中读取属性,你可以使用

Properties prop = new Properties();
InputStream input = this.getClass().getResourceAsStream("/application.properties")
prop.load(input);

// get the property value and print it out
System.out.println(prop.getProperty("foo"));

【讨论】:

    【解决方案4】:

    对于想要扫描应用程序类路径外部属性的非启动用户:

    @PropertySource("file:/path/to/application.properties") 对于网络托管的远程属性,“文件”可以替换为“http”

    【讨论】:

      【解决方案5】:

      我使用下一个选项从任何地方加载属性文件,并将其放入环境中以通过Environment#getProperty@Value("name") 访问它:

      @Configuration
      public class MVCConfig {
      
          @Autowired
          private ConfigurableEnvironment env;
      
          @PostConstruct
          public void setup() {
             Properties config = new Properties();  
             try (InputStream stream = this.getClass().getResourceAsStream("/custom.properties")) {
                 config.load(stream);
             }
             env.getPropertySources().addLast(new PropertiesPropertySource("mvc", config));
          }
      }
      

      【讨论】:

      • 嗨@TheDarkDnKTv,欢迎来到 Stack Overflow 并感谢您的贡献。我认为您可能误解了这个问题,他们正在寻找如何从外部来源(例如文件)加载属性。所以他们想要您评论的这部分:/* 此处的属性加载代码。您可以使用默认的 Java */.
      猜你喜欢
      • 2016-11-16
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 2017-06-04
      • 1970-01-01
      • 2016-11-29
      相关资源
      最近更新 更多