Spring Boot comes with a built-in mechanism for application configuration using a file called application.properties. In this article, I’ll show you how to effectively use the application.properties file in custom scenarios.
I’m not going to discuss properties specified by the Spring Boot framework. Working with existing configuration keys is pretty straightforward. You can easily find common keys in the official documentation.
This post covers defining custom properties, handling data types, and working with properties on different runtime environments. If that’s what you’re looking for, keep on reading.
Continue reading “Configuring Spring Boot with application.properties file”
Saving enum values in a relational database is a bit more tricky than primitives, Strings, and regular immutable value wrappers. From this post, you’ll learn different methods for saving Java enums in a relational database using JPA. I’ll compare these methods and present the best application for them.
Continue reading “How to save enum in database with JPA”
Every programmer agrees naming classes is highly important for code readability. Proper naming decrease the time needed to understand the code base. Many developers will also agree that class naming isn’t easy. There are numerous queries around the best practices which come not only from the beginners.
The aim of this article is to put in one place answers for the most popular questions around Java class name conventions and community standards. I’ll cover technical Java language restrictions, common conventions, and popular class naming best practices.
So much by way of introduction. Let’s get down into it.
Continue reading “Java class naming – The ultimate guideline”
The object equals contract indicates that when two objects are equal, their hash codes must also be the same. It’s a general agreement for all Java objects used in hash-based collections. Its main purpose is to optimize performance when working e.g. with HashMap or HashSet.
You may hear that when you implement the equals() method for your class, you should also implement the hashCode() method. That’s the practical approach for fulfilling the equals contract.
If you want to know more details why the contract is so important, keep on reading.
Continue reading “What is Java object equals contract?”
Is Spring controller/service/singleton thread-safe?
It’s a commonly asked question by Spring newcomers and probably a must-have warm-up question on job interviews. As usual in programming, the answer is: it depends. The main factor which determines thread safety of a component is its scope.
Let’s get down to it and see what Spring’s scopes have to offer in multithreaded programming.
Continue reading “Spring bean thread safety guide”
Spring Boot does a lot of configuration automatically for us but sooner or later you’ll have to do some custom work. In this post, you will learn how to hook into the application bootstrap lifecycle and execute code on Spring Boot startup.
So let’s see what the framework has to offer.
Continue reading “Running code on Spring Boot startup”
When you design a method in a Java class, some parameters may be optional for its execution. No matter it is inside a DTO, a fat model domain object, or a simple stateless service class, optional method parameters are common.
From this article you will learn how to handle optional parameters in Java. We’ll focus on regular method, class constructors with optional fields, and quickly look at bad practices of the discussed topic. We’ll stop for a moment to look at Java 8 Optional and assess if it fits our needs.
Let’s get started.
Continue reading “Java optional parameters in practice”
With a small amount of work, you can speed up your application and improve the overall user experience. In this article, you will learn how to use the built-in HTTP response cache mechanism for Spring controller’s results.
Continue reading “HTTP cache with Spring examples”
Writing parameterized tests in JUnit 4 was pretty cumbersome. JUnit 5 introduced several useful improvements to the framework and running the same test with different arguments is much simpler than in the previous version. However, there is one small issue with passing null values in such arguments.
In this post, I’m going to show you how to pass null in @CvsSource and @ValueSource for @ParametrziedTest in JUnit 5.
Continue reading “@ParameterizedTest with null values in @CvsSource”
Do you have multiple parameters annotated with @RequestParam in a request mapping method and feel it isn’t readable?
The annotation looks pretty straightforward when there’s one or two input parameters expected in a request but when the list gets longer you might feel overwhelmed.
You cannot use the @RequestParam annotation inside objects but it doesn’t mean you’re left with no other solution. In this post, I’m going to show you how to replace multiple @RequestParams with an object.
Continue reading “How to bind @RequestParam to object in Spring”