Configuring Spring Boot with application.properties file

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”

Java class naming – The ultimate guideline

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”

What is Java object equals contract?

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?”

Spring bean thread safety guide

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”

Java optional parameters in practice

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”

HTTP cache with Spring examples

Caching is a powerful feature of the HTTP protocol but for some reason, it’s mainly considered for static resources like images, CSS stylesheets, or JavaScript files. However, HTTP caching isn’t limited to application’s assets as you can also use it for dynamically computed resources.

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”

@ParameterizedTest with null values in @CvsSource

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”

How to bind @RequestParam to object in Spring

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”