Spring

Spicy Spring: Dockerize Spring Boot Application With Jib

Posted on by  
Hubert Klein Ikkink

Jib is an open-source Java library from Google for creating Docker images for Java applications. Jib can be used as Maven or Gradle plugin in our Spring Boot project. One of the nice feature of Jib is that it adds layers with our classes, resources and dependency libraries for the Docker image. This means that when only class files have changed, the classes layer is rebuild, but the others remain the same. Therefore the creation of a Docker image with our Spring Boot application is also very fast (after the first creation). Also the Maven and Gradle plugins have sensible defaults, like using the project name and version as image name, so we don’t have to configure anything in our build tool. Although Jib provides options to configure other values for the defaults, for example to change the JVM options passed on to the application.

Continue reading →

Spring Sweets: Access Application Arguments With ApplicationArguments Bean

Posted on by  
Hubert Klein Ikkink

When we start a Spring Boot application and pass arguments to the application, Spring Boot will capture those arguments and creates a Spring bean of type ApplicationArguments and puts it in the application context. We can use this Spring bean to access the arguments passed to the application. We could for example auto wire the bean in another bean and use the provided argument values. The ApplicationArguments interface has methods to get arguments values that are options and plain argument values. An option argument is prefixed with --, for example --format=xml is a valid option argument. If the argument value is not prefixed with -- it is a plain argument.

Continue reading →

Spring Sweets: Custom Exit Code From Exception

Posted on by  
Hubert Klein Ikkink

When we write a Spring Boot application a lot of things are done for us. For example when an exception in the application occurs when we start our application, Spring Boot will exit the application with exit code 1. If everything goes well and the we stop the application the exit code is 0. When we use the run method of SpringApplication and an exception is not handled by our code, Spring Boot will catch it and will check if the exception implements the ExitCodeGenerator interface. The ExitCodeGenerator interface has one method getExitCode() which must return a exit code value. This value is used as input argument for the method System.exit() that is invoke by Spring Boot to stop the application.

Continue reading →

Spring Sweets: Add Git Info To Info Endpoint

Posted on by  
Hubert Klein Ikkink

With Spring Boot Actuator we get some endpoints that display information about our application. One of the endpoints is the /info endpoint. If our project uses Git we can add information about Git to the /info endpoint. By default Spring Boot will look for a file git.properties in the classpath of our application. The file is a Java properties file with keys that start with git. and have values like the branch name, commit identifier and commit message. Spring Boot uses this information and when we request the /info endpoint we get a response with the information. This can be very useful to check the Git information that was used to build the application. To create the git.properties file we can use a Gradle (or Maven) plugin that will do the work for us.

Continue reading →

Spicy Spring: Running Our Own Spring Initializr Server

Posted on by  
Hubert Klein Ikkink

To start a new project based on Spring or Spring Boot we can use the website start.spring.io. We can easily create a project templates based on Maven or Gradle and define all needed dependencies by clicking on checkboxes in the UI. In a previous post we also learned how to create a project using a URL using the same start.spring.io website. The start.spring.io website is actually a Spring Boot application and we can easily host our own server. With our own server we can for example limit the number of dependencies, force Gradle as the only build tool and set default values for project name, packages and much more.

Continue reading →

Spring Tip: Check for existence of Bean during startup

Posted on by  
Willem Cheizoo

Sometimes we are working on an application where we have no control over specific Spring bean implementations. Nevertheless we want to check for the existence (or duplication) of these bean implementations during start-up of the application server. Since Spring version 3.0 it is possible to use Spring's ApplicationListener. This interface is based on the EventListener and uses the standard Observer design pattern. In 3 steps we can easily check for the existence of a specific Spring bean.

Continue reading →

shadow-left