Adding Some Demo Data

Before we can start displaying data in our application we need to add some demo data. You could point the data source to be an external persistent data source that contains test data instead of using an in-memory data source that is lost each time to stop the application or you could load up the in-memory data source with demo data each time the application is started by using either a sql file containing statements to add the data or by writing a script that runs when the application starts that uses the repositories that we built earlier.

For this demo data I’m going to use the data repositories method as it is quick and easy and doesn’t require any sql knowledge.

Lets create a new class called DemoData that implements a Spring class called Application Runner. The class is also annotated with @Component which will allow it to be detected when the Spring Boot application starts.

Next we need access to the repositories. This is done by Autowiring them. Autowiring is a way to setup bean references in your code. Think of this as something similar to managed beans in XPages except you don’t need to edit any xml to get them working. Below is a constructor based autowire of the two repositories.

First I create two private variables in the class that will hold the beans. Next I create a constructor for the class that takes in a parameter for each bean I’m autowiring and then those to the private variables. Lastly I need to make sure I add the @Autowired annotation to the constructor. I could also have written the auto wiring as follows

but this is no longer recommended. If you see code examples like this then you can easily turn them in to constructor autowiring by allowing IntelliJ to convert them for you. Just click the code hint icon that appears when you click on the annotation and pick the constructor.

Now we just need a method that will run when the application runs. Because we extended ApplicationRunner there is a method that we can override called ‘run’ and then we can add as much data to the repositories as we like.

Here I have added one Location and one Person You can add as many as you want in your version. I do like the way that IntelliJ shows the argument names, this makes it really easy for when you next look at the code in a few months time.

One question you might ask yourself is how do we stop this ApplicationRunner from running once the application is in production as we would not want that data showing in that environment. The answer is simple and is all to do with Profiles. I’ll cover profiles in a later entry when we get around to deployments.

Now that we have data we can add it to our UI…

Tagged with: , , , ,
Posted in Domino To Spring, None
%d bloggers like this: