Posts by Willem Cheizoo

Spicy Spring: Write your own AutoConfiguration

Posted on by  
Willem Cheizoo

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.

Continue reading →

Spicy Spring: Inject your custom method argument in Spring MVC using HandlerMethodArgumentResolver

Posted on by  
Willem Cheizoo

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:

Continue reading →

Spicy Spring: Create your own ResourceLoader

Posted on by  
Willem Cheizoo

As a Spring developer we know how to load a resource in a Service. The prefixes of classpath: and file: are commonly used. But what if we want to load resources from a different location like a database table RESOURCE, with our own prefix db:? The Spring Framework provides some default built-in resource implementations, which can be found in the chapter Resources in the Spring Framework Reference Guide. The URL is prefixed and handled by the corresponding ResourceLoader (JavaDoc). If we want to load a resource from a database table RESOURCE we have to create our own ResourceLoader which triggers on the prefix db:.

Continue reading →

Spicy Spring : Dynamically create your own BeanDefinition

Posted on by  
Willem Cheizoo

When we a have Spring managed application, we want to let Spring manage all of our beans. Beside the regular way of creating beans with known solutions like Annotated beans, Java Configuration and XML Configuration, there is also a way in which we can create our own BeanDefinition. With a BeanDefinitionRegistryPostProcessor it is possible to create a specific post processor which can add BeanDefinitions to the BeanDefinitionRegistry. It differs from the BeanPostProcessor, which only has hooks for Bean Initialization (construction of your POJO), where the BeanDefinitionRegistryPostProcessor has a hook on the BeanDefinitionRegistry. This gives us the ability to define our own BeanDefinition. First we create a BeanDefinitionRegistryPostProcessor implementation as listed in the example. We implement the required method, and will be able to add our own bean definition to the registry. The defined BeanDefinition will be picked up by the ApplicationContext and the POJO will be constructed. Our result is A Spring managed bean

Continue reading →

Construct a typed Array via List.toArray() with correct size

Posted on by  
Willem Cheizoo

When we construct an typed Array out of an existing List, we use the method T[] toArray(T[] a). When an array with a lower size than the size of the List is passed as argument, this results in a new array being created. Take a look at the implementation of ArrayList here. Using the method with an incorrect sized array is inefficient. Using the toArray method directly with a correctly sized array is therefore preferable.

Continue reading →

Using Spring managed Bean in non-managed object

Posted on by  
Willem Cheizoo

We have to deal with legacy code, even when we would like to use the best and newest technologies available. Imagine the new code is written with the newest technologies of the Spring Framework and the legacy code is not written in Spring at all. Then using Spring managed Beans in non-managed Spring objects is one of the patterns we have to deal with. The legacy code has non-managed Spring objects, while the code we want to reference to is a Spring managed Bean. How do we solve this problem?

Continue reading →

Prevent 'No plugin found' in multi-module maven

Posted on by  
Willem Cheizoo

Defining a maven plugin on a submodule in a multi-module maven project can give us a 'No plugin found' error. Especially if we have a multi-module project and we want to apply a maven plugin in only one specific module this error occur pretty often. Let's say we have a multi-module root pom which looks like this.

Continue reading →

Nifty JUnit : How to test for an exception

Posted on by  
Willem Cheizoo

Testing for exceptions in JUnit is something we have to deal with! We want to test if an exception occurs in a particular situation, or even if the exception contains a particular message. The question is: How to test for an exception in Junit? What we see very often is this:

Continue reading →

shadow-left