Check whether you have multiple ContextLoader* definitions in your web.xml!

If you are using Spring in your web project and just added Spring Security, you may come across this error message when trying to start the application.

Continue reading

7 Essential Eclipse Plugins

Every developer has a list of his or her useful Eclipse plugins. This is my list. This list is not meant to be complete and comprehensive. It is highly biased and opinionated list. There a lot of great plugins out there but this list falls under the “must have” for me personally.

1. Springsource Tool Suite (STS)

If you are using Spring in your development, STS is a must have. Ctrl+Shift+L feature (for quick search) alone is a great reason to use STS in your daily work.

2. StartExplorer Eclipse Plug-in

I found this plug-in to be great for quick access to a file outside of Eclipse. “This plug-in offers tight integration of system file manager (Windows Explorer, Gnome Nautilus, KDE Konqueror, Mac Finder, …) and shell (cmd.exe, Linux/Mac terminal) in Eclipse.”

Continue reading

Spring’s @Transactional Important Points

Spring has a great way to handle transactions using @Transactional annotation. However, there are a few gotchas that one needs to watch out for. Without a good understanding of how Spring treats transactions, you can run into some unexpected and undesirable behavior. For an in depth study of the topic refer to the Sping Spring Documentation on transaction management. I will try to summarize a few important points.

1. Spring provides support for transactions using AOP Proxies

Spring AOP Proxy http://docs.spring.io/spring/docs/4.0.2.RELEASE/spring-framework-reference/htmlsingle/#tx-decl-explained

It means that you are not calling a method directly but through a proxy. This is a very important point to remember when defining transaction boundaries. In an event where a service method calls another method in the same class with a different propagation setting (PROPAGATION_REQUIRES_NEW for example), that setting will be ignored since the call is made internally. If you are not aware of the AOP concept the behavior of those methods will most likely not match your expectations.

In order to let Spring manage transactions you have to add @EnableTransactionManagement to your Java config class or if using XML configuration add <tx:annotation-driven/> . (Don’t forget the namespace: xmlns:tx="http://www.springframework.org/schema/tx"

Continue reading

Converting JSON object to a generic class with Jackson

Marshaling a Java Object to JSON is very straight forward. Here is a simple example that prints out the object’s JSON representation.

Converting a JSON String to a Java Object is similarly simple.

Sometimes, however, it is necessary to unmarshal a JSON object into a generic class. To do that we need to use TypeReference.class.

To convert JSON to a more complicated object, for example a list List<MyObject> we would need to construct a collection type. API Doc JavaType.class

For more information refer to the official Jackson Main Page.

@TestExecutionListeners is not present for class

As I was writing and running some integration tests I came across this error message.

Turns out I did not have @Test annotation on a method I was trying to run as a JUnit test. The interesting thing I only got that error when other methods in that test class were annotated with @Test. If there were no other methods it would throw a "No runnable methods exception". Completely trivial but I did not immediately connect the error message to the actual cause.

TestExecutionListener API Doc