简体   繁体   English

如何使用 Spring 从属性文件中读取

[英]How to read from a properties files using Spring

I'd like to use Spring to read from a properties file retrieving a URL, a username, and a password.我想使用 Spring 从检索 URL、用户名和密码的属性文件中读取。 I've read many examples online and they all more or less look the same but I simply don't understand them.我在网上阅读了很多例子,它们或多或少看起来都一样,但我根本不理解它们。

One said example shows two similar methods using the @value annotation and another is using Environment env yet every example using the latter says to use env.getProperty() but that method doesn't seem to exist for that object?一个所述示例显示了使用 @value 注释的两个类似方法,另一个使用Environment env但使用后者的每个示例都说使用env.getProperty()但该方法似乎不存在该对象? Using @value I don't understand the method called sampleService .使用 @value 我不明白名为sampleService的方法。 Am I supposed to create an object class?我应该创建一个对象类吗?

     @Configuration
     @PropertySource("classpath:src/main/resources/config.properties")
     public class EnvironmentConfig {

    @Value("${config.properties}")
     public static String url;

    @Value("${config.properties}")
    public static String username;

    @Value("${config.properties}")
    public static String password;

    @Bean
    public static DataSource logInSetup() {

        DriverManagerDataSource login = new DriverManagerDataSource();
        login.setUrl(url);
        login.setUsername(username);
        login.setPassword(password);

        return login;
     }

     @Bean
     public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {

      return new PropertySourcesPlaceholderConfigurer();

I don't think the above is even remotely close.我不认为上面的内容甚至很接近。 I'm sure this is more simple them I'm making it out to be.我相信这更简单,我正在做。

If your properties file is something like:如果您的属性文件类似于:

url=...
username=...
password=...

You can write something like:你可以这样写:

@Configuration
@PropertySource("classpath:src/main/resources/config.properties")
public class EnvironmentConfig {

  @Bean
  public DataSource logInSetup(@Value("${url}") String url, @Value("${username}") String username, @Value("${password}") String password) {

        DriverManagerDataSource login = new DriverManagerDataSource();
        login.setUrl(url);
        login.setUsername(username);
        login.setPassword(password);
        return login;
   }
}

You should remove static everywhere.你应该到处去除static Avoid using static with Spring.避免在 Spring 中使用静态。

And you don't need PropertySourcesPlaceholderConfigurer as a bean.而且您不需要PropertySourcesPlaceholderConfigurer作为 bean。 that's what PropertySource annotation should do.这就是 PropertySource 注释应该做的。 Anyway, avoid using new with Spring either.无论如何,避免在 Spring 中使用new Especially for Spring classes.特别是对于 Spring 类。 If you are doing it, most likely it's a bug.如果你这样做,很可能是一个错误。

Btw, most likely the url to the config.properties is incorrect and should be just @PropertySource("classpath:config.properties") or maybe @PropertySource("classpath:/config.properties")顺便说一句,很可能 config.properties 的 url 不正确,应该只是@PropertySource("classpath:config.properties")或者@PropertySource("classpath:/config.properties")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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