Last Updated: February 25, 2016
· gavinbunney
Gravatar 005

Bootstrapping data with Spring

So you find yourself working on a 'traditional' Java Spring project, and you need some reference data pre-populated into your database.

In Grails this is quite easy - just crack open bootstrap.groovy and start creating new entities.

Fortunately, we can create a similar mechanism using an InitializingBean and a bit of pre-conditional checking.

Bean Class

In our example application, we need a system user and a bunch of country codes added to our database. This can always be expanded to read lists from xml/csv files, but keeping things simple for this example.

Each individual function is responsible for checking if it's data is required to be bootstrapped, or if it always has been.

public class BootstrapDataPopulator implements InitializingBean {

    private final Logger LOG = LoggerFactory.getLogger(BootstrapDataPopulator.class);

    UserRepository userRepository;

    CountryCodeRepository countryCodeRepository;

    public void afterPropertiesSet() throws Exception {"Bootstrapping data...");

        createCountryCodes();"...Bootstrapping completed");

    private void createSystemUser() {
        if (userRepository.getSystemUser() != null) {
        }"... creating system user");

        User user = new User();

    private void createCountryCodes() {
        if (countryCodeRepository.count() > 0) {
        }"... creating Country codes");

        List<CountryCode> codes = new ArrayList<>();
        codes.add(new CountryCode(1, "AU", "Australia"));
        codes.add(new CountryCode(2, "USA", "United States of America"));
        codes.add(new CountryCode(3, "NZ", "New Zealand"));;

Bean Declaration

In the above example, we are using the @Service annotation to initialize the bean, but there is no reason you can't declare the bootstrap bean more explicitly.

Logs for Logging logs

The advantage of the 'individual function' sort of pattern is you easily see what parts of the bootstrapping process are occuring

E.g. on first run:

Bootstrapping data...
... creating system user
... creating Country codes
...Bootstrapping completed

And subsequent runs:

Bootstrapping data...
...Bootstrapping completed
Say Thanks