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?
Let's assume we have a managed Spring Bean called TaxService
and an object called LegacyObject
. The LegacyObject
is the legacy code from where we would make a reference to the method calculateTax
on the managed Spring Bean.
Continue reading →
I get this question quite often and I struggled with it many times before: How do I work with temporary files in my JUnit testcases? I would like to explain two simple ways of working with temporary files in JUnit.
Create a temporary file with the Java File API and mark it directly as deleteOnExit()
. The created file will be deleted when the JVM exits.
Continue reading →
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.
4.0.0
com.jdriven.blog
maven-plugin-multimodule
0.1-SNAPSHOT
pom
module1
module2
Continue reading →
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:
import org.junit.Test;
public class JDrivenServiceTest {
JDrivenService service = new JDrivenService();
@Test(expected = ArticleNotFoundException.class)
public void testPublishArticle_WithException() throws Exception {
service.publishArticle(null);
}
}
Continue reading →
When I was recently working on a large restructuring and refactoring where I also replaced Ant by Maven, it was really necessary to compare the complete content of two different JAR files. It was required to know that the result of the restructuring and refactoring hadn't changed the artifacts, thus the JAR files. In the JAR files were different Class files present. When I compared the content of the two JAR files (with a binary compare) all the content was radically changed. This was partly because the compiler compiled the Class files at a different timestamp.
Since I wanted the best possible comparison between the two JAR files I needed to compare all Class files in the JAR by decompiling and comparing. This should give me a clearer and more honest picture of the differences. For this action I used Beyond Compare. By using an additional File Format (Java Class to Source) I was able to completely compare the decompiled Class files of the two JARS.
Continue reading →
Sometimes we are working on an application where we have no control over specific Spring bean implementations. Nevertheless we want to check for the existence (or duplication) of these bean implementations during start-up of the application server. Since Spring version 3.0 it is possible to use Spring's ApplicationListener. This interface is based on the EventListener and uses the standard Observer design pattern. In 3 steps we can easily check for the existence of a specific Spring bean.
We create an ApplicationListener<ContextRefreshedEvent> implementation. The method onApplicationEvent will be called when a refresh of the ApplicationContext occurs.
package com.jdriven.blog.applicationlistener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
public class BeanContextStartupListener
implements ApplicationListener<ContextRefreshedEvent>
{
public void onApplicationEvent( ContextRefreshedEvent event )
{
...
}
}
We need to register the BeanContextStartupListener as a Spring Component. In this sample we just simply enable component scanning in the spring xml configuration as follows.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.jdriven.blog.applicationlistener" />
</beans>
We also add the @Component
to the class definition:
import org.springframework.stereotype.Component;
@Component
public class BeanContextStartupListener
implements ApplicationListener<ContextRefreshedEvent>
We implement the onApplicationEvent method of the BeanContextStartupListener and throw an Exception when no bean exists within the current ApplicationContext.
public void onApplicationEvent( ContextRefreshedEvent event )
{
Map serviceImplementations = event.getApplicationContext().getBeansOfType( JDrivenService.class );
if ( serviceImplementations.isEmpty() ) {
throw new IllegalStateException("JDrivenService was not instantiated during startup.");
}
}
Continue reading →