Autowiring Property Files using Spring

It’s often the case that credentials and URLs of external services need to be accessible in your application. These values can’t be stored in your version control for security reasons. One solution is to put the values in a Java properties file, store only a template for that file in your version control, and put the actual properties file on your installed instance.

Here’s how to access the properties file values in your Java code using Spring configuration:

@Configuration
@PropertySource("classpath:that-api.properties")
public class ThatApiProperties {

    @Value("${login}")
    private String login;

    @Value("${password}")
    private String password;

    @Value("${uri}")
    private String uri;

    public String getLogin() {
        return this.login;
    }

    public String getPassword() {
        return this.password;
    }

    public String getUri() {
        return this.uri;
    }
}

The properties file for this example needs to be in your project directory in the following subdirectory:

src/main/resources/that-api-TEMPLATE.properties

During your build process, you’ll create a version of this file with the sensitive values plugged in and save it to

src/main/resources/that-api.properties

A React “Hello, World!” Example

This github repository shows you how to set up a nice little development environment for React. First, copy the files to a working directory. Then run

npm i
npm start

The first command will initialize your project (something you only need to do the first time.) The second command will start a web server that will serve up your files locally.