Posts

Showing posts from July, 2014

Spring 4.1 and Java 8: java.util.Optional as a @RequestParam, @RequestHeader and @MatrixVariable in Spring MVC

Image
As of Spring 4.1 Java 8’s java.util.Optional , a container object which may or may not contain a non-null value, is supported with @RequestParam , @RequestHeader and @MatrixVariable . While using Java 8’s java.util.Optional you make sure your parameters are never null .

JUnit: testing exception with Java 8 and Lambda Expressions

Image
In JUnit there are many ways of testing exceptions in test code, including try-catch idiom , JUnit @Rule , with catch-exception library. As of Java 8 we have another way of dealing with exceptions: with lambda expressions. In this short blog post I will demonstrate a simple example how one can utilize the power of Java 8 and lambda expressions to test exceptions in JUnit.

Spring 4: CGLIB-based proxy classes with no default constructor

Image
In Spring, if the class of a target object that is to be proxied doesn’t implement any interfaces, then a CGLIB-based proxy will be created. Prior to Spring 4, CGLIB-based proxy classes require a default constructor. And this is not the limitation of CGLIB library, but Spring itself. Fortunately, as of Spring 4 this is no longer an issue. CGLIB-based proxy classes no longer require a default constructor. How can this impact your code? Let’s see. One of the idioms of dependency injection is constructor injection. It can be generally used when the injected dependencies are required and must not change after the object is initiated. In this article I am not going to discuss why and when you should use constructor dependency injection. I assume you use this idiom in your code or you consider using it. If you are interested in learning more, see the resources section in the bottom of this article. Contructor injection with no-proxied beans Having the following collaborator: packa...