Natural Programmer's Blog

Topics on Spring Framework for developing elegant real-world applications

Spring Framework: How to Read Resources Using @Value Annotation

When developing applications, many times we need to read resources – e.g., files from the classpath, a URL, or the file system. To make our job easy, Spring provides a Resource interface with many built-in implementations. Reading resources using the built-in Resource implementations is easy,...


Spring Framework – how to execute some code only after successful commit

When coding a Spring application, we will typically be annotating our service methods with @Tansactional, and sometimes we would want to execute come code only if the transaction succeeds. For example, when a user signs up, she should receive a verification mail only if her record is successfully...


How to run some code when a Spring application starts

It’s often necessary to run some startup code when an application starts. For example, in a test environment, you can add some test records. Or, you can add an admin user on new installations. There are many to do this in a Spring application. Let’s discuss a couple. Using...


Spring Boot – how to configure in-memory database for testing

While running automated tests, we’d often like to use a light-weight in-memory database instead of a persistent one like MySQL. So, in this post, we’ll discuss how to configure your Spring Boot application to use MySQL in development (or production), whereas in memory HSQL when running automated...


Spring Boot – How Do You Externalize Application Properties

Application properties, e.g. the database connection parameters, shouldn’t be hard-coded in our applications. They should instead be put externally, say in properties/YML files, and injected into the application. Spring Boot has multiple ways to handle this. It’s detailed in the reference...


Spring Framework REST API validation – are you doing it correctly?

When developing REST Web Services, you should validate the input parameters and return a 4xx response in case of invalid data. The body of the response should contain field-wise error details in a structured format, so that clients can show that to the end-users nicely. Let’s take an example....