In Spring we use the @EnableAutoConfiguration
each time when we use the @SpringBootApplication
annotation.
If we look at the @SpringBootApplication
we can see that this automatically enables the @EnableAutoConfiguration
.
This last mentioned annotation triggers all the auto-configuration enabled configurations on the classpath.
We can write an auto-configuration enabled @Configuration
ourself in only two steps.
package com.jdriven.example;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyOwnAutoConfiguration {
//You can define your own beans here and
//further setup this Configuration as you normally would do
}
Continue reading →
In Spring MVC we get some method argument types resolved by default and injected in Spring MVC controller methods.
Some examples are Model
, Locale
and OutputStream
.
What if we want to inject a custom argument in Spring MVC controller methods?
In this example we extract the X-Application-Version
HTTP header from the request and inject that as a method argument called version
.
Our controller class will look like the following:
@RestController
public class MyController {
@RequestMapping("/persons")
//I want the version to be automatically injected
public List getPersons(String version) {
.....
return someList;
}
}
Continue reading →
In a previous post we learned how to add an extra DataSource
to our Ratpack application.
At that time on the Ratpack Slack channel there was a discussion on this topic and Danny Hyun mentioned an idea by Dan Woods to use a Map
with DataSource
objects.
So it easier to add more DataSource
and Sql
objects to the Ratpack registry.
In this post we are going to take a look at a solution to achieve this.
We are going to use the HikariDataSource
, because it is fast and low on resources, in our example code.
First we create a new class to hold the configuration for multiple datasources.
The configuration is a Map
where the key is the name of the database and the value an HikariConfig
object.
The key, the name of the database, is also used for creating the HikariDataSource
and Sql
objects.
And the good thing is that Ratpack uses a Jackson ObjectMapper
to construct a configuration object and it understands Map
structures as well.
In the ratpack.groovy
file at the end of this blog post we see how we can have a very clean configuration this way.
Continue reading →
Recently on our project where we use Ratpack we had to get data from different databases in our Ratpack application.
We already used the HikariModule
to get a DataSource
to connect to one database.
Then with the SqlModule
we use this DataSource
to create a Groovy Sql
instance in the registry.
In our code we use the Sql
object to query for data.
To use the second database we used the Guice feature binding annotations to annotate a second DataSource
and Sql
object.
In this post we see how we can achieve this.
Interestingly while I was writing this post there was a question on the Ratpack Slack channel on how to use multiple datasources.
The solution in this post involves still a lot of code to have a second DataSource
.
In the channel Danny Hyun mentioned a more generic solution involving a Map
with multiple datasources.
In a follow-up blog post I will write an implementation like that, so we have a more generic solution, with hopefully less code to write.
BTW the Ratpack Slack channel is also a great resource to learn more about Ratpack.
Continue reading →
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.
To get started we must first clone the GitHub project.
We need to build the project with Maven to install the libraries in our local Maven repository.
After that is done we are reading to use it in our own Spring Boot application that is our customised Spring Initializr server.
The easiest way to run the server is to have the Spring CLI tool installed.
The easiest way to install it is using SDKMAN!.
We type on the command line $ sdk install springboot
.
Continue reading →
GitHub supports Asciidoctor markup.
We can add a document to GitHub with the extension adoc
and it is parsed and the resulting HTML is shown when we view the document in our web browser in a GitHub repository.
In March 2016 Dan Allen tweeted about enabling admonition icons on GitHub.
A follow-up tweet by Ted Bergeron mentioned more examples.
In this post we see an example on how to use their suggestions.
Normally we wouldn't see an icon, but we can use document attributes and assign an emoji for each admonition type.
For example a note admonition icon is set with the document attribute note-caption
.
We can use a condition check to see if the document is rendered on GitHub and only then use the document attributes.
In the following example we assign icons to the different admonitions:
Continue reading →
If we have a Gradle task of type Test
we can use a filter on the command line when we invoke the task.
We define a filter using the --tests
option.
If for example we want to run all tests from a single package we must define the package name as value for the --tests
option.
It is good to define the filter between quotes, so it is interpreted as is, without any shell interference.
If we configure the test
task to output the test class name we can see that which tests are executed.
In the following snippet we reconfigure the test
task:
Continue reading →
If we want to see how productive we are with IntelliJ IDEA we must open Help | Productivity Guide.
IntelliJ IDEA opens a dialog window with a very detailed overview of smart stuff we have been using.
And how much typing we have saved.
Continue reading →
Usually in our Ratpack application we use a registry to store components that we want to use in our application code.
The calling code for the registry doesn't need to know how the registry is implemented.
Ratpack support Google Guice for example, but Spring is also supported.
This means we can define the components for our registry using Spring and we only have to tell Ratpack where to look for the Spring configuration files.
Ratpack provides for us the ratpack.spring.Spring
class with the static method spring
.
This method returns a Ratpack registry implementation we can use in our application.
To enable Spring support we must add the Ratpack spring-boot dependency to our build file:
Continue reading →
If you have read my previous post about caching, The (non)sense of caching, and have not been discouraged by it, I invite you to build your own cache.
In this post we will build a simple cache implementation that refreshes its data automatically, using Java EE features.
Let's describe the situation.
We are building a service that uses an external resource with some reference data.
The data is not frequently updated and it's allright to use data that's up to 1 hour old.
The external resource is very slow, retrieving the data takes about 30 seconds.
Our service needs to respond within 2 seconds.
Obviously we can't call the resource each time we need it. To solve our problem we decide to introduce some caching.
We are going to retrieve the entire dataset, keep it in memory and allow retrieval of specific cached values by their corresponding keys.
Continue reading →