-1

I'd like to use Spring to read from a properties file retrieving a URL, a username, and a password. 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? Using @value I don't understand the method called 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.

WeVie
  • 568
  • 2
  • 9
  • 25

1 Answers1

3

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. Avoid using static with Spring.

And you don't need PropertySourcesPlaceholderConfigurer as a bean. that's what PropertySource annotation should do. Anyway, avoid using new with Spring either. Especially for Spring classes. 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")

Tarlog
  • 10,024
  • 2
  • 43
  • 67
  • even with this I'm still not clear how to extract the values from the returned object. What should that look like when I call this ? Do I need to create get methods? – WeVie Mar 05 '20 at 14:19
  • I think I wrote exactly how you extract the values: You can add Value annotation with property name to the parameters of the method annotated with Bean. The other way to do it, is similar to what you did: add Value to configuration class members. Make sure they are NOT static. – Tarlog Mar 05 '20 at 18:53
  • I mean how do I extract them from the object login since there's no get methods? – WeVie Mar 05 '20 at 19:02
  • Are you asking how to extract them from DriverManagerDataSource after you set it? No idea, what is DriverManagerDataSource? If you need to access to these data elsewhere, why not setting is elsewhere? Anyway, it's not a Spring question. Spring allows you extracting the properties. What you are doing after is up to you. – Tarlog Mar 05 '20 at 19:15
  • Yes that's what I'm asking. I can't find anything online that show how to actually use the data once it's read from the properties file – WeVie Mar 05 '20 at 19:43