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.