Tailor-Made Neo4j Connectivity With Spring Boot 2.4+

Spring Boot has support for a broad range of properties to configure the Neo4j Java Driver since Spring Boot 2.4. The Neo4j team worked together with VMWare engineers to make this as integrated as possible.

What does that mean? You can either go to start.spring.io and add “Neo4j” as a dependency which will give you the driver plus Spring Data Neo4j.

or you can add the driver dependency manually like this:

Please note that the version number should be omitted when you are using Spring Boots provided parent org.springframework.boot:spring-boot-starter-parent.

Why? Because Spring Boot manages dependencies for your project, among them, the Neo4j Java driver. Thus, you always have the latest, compatible version.

In the rare cases you need to change, use the following property: <neo4j-java-driver.version>4.4.3</neo4j-java-driver.version>.

This does not only apply to our dependency, but in general: In case a dependency comes with more than one artefact, Spring Boots dependency management makes sure you catch them all. If you are using Gradle, the same principles apply.

You don’t need to write any code to configure the Neo4j connection in the vast majority of use cases. For all the basic needs, just enter three essential properties (URL, username, and password) and you’re good to go:

Spring Boot Properties for Neo4j

You don’t have to write a single line of config code yourself to configure any of this. This is important, as we often see a lot of code written to just do something that is already supported out of the box.

If you roll out your own configuration, most of the time you will lose benefits such as: Validation of properties and properties that are agnostic to the sources (the configuration shown above can originate from property files, YAML files, environment variables, command line parameters or config servers, just to name a few).

There are only a small number of things you can’t configure from properties. How to deal with those situations? One way is to inject org.springframework.boot.autoconfigure.neo4j.Neo4jProperties into an @Bean annotated method on a @Configurationclass and first construct the org.neo4j.driver.Config and then the database driver yourself and return the latter as a bean.

This way you keep Spring Boot’s awesome configuration sources and can reuse the standard properties. However, that’s a lot of work to do and you are in danger of forgetting one essential property to copy over to that manual config.

Tailor-Made All the Things

Photo by Beatriz Moraes on Unsplash

The ConfigBuilderCustomizer is the correct way to go. It’s an official part of Spring Boot. Provide it as Spring Bean and it will receive a builder for configuration required by the Neo4j Java driver with all properties applied that still allows adding new ones or even overwriting some. In a contrived example, that would look like this, showing a couple of additional options for which we don’t provide Spring configuration properties out of the box:

Happy hacking and make sure you try out Neo4j AuraDB, our cloud offering which works from a broad range of ecosystems, including Java and Spring Boot.

Tailor-Made Neo4j Connectivity With Spring Boot 2.4+ was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.