Skip to main content

Posts

Showing posts from June, 2017

Container ready spring boot application

Spring boot applications are now ubiquitous. The usual way to build one is to create an uber jar. At the same time, Docker allows us to build self reliant containers which are unaffected by the underlying server architecture or neighboring applications or their dependencies. Spring boot applications can also run in docker containers.  However running an uber jar inside a container fails to satisfy an important goal. That of high speed build and deployment. An uber jar is a heavy weight entity. That will make docker image heavy and slow to build. Here's a step by step solution, which leverages docker layer caching feature for faster builds with all spring boot goodness. We will use Maven to build a deployment structure for our docker image that allows fast deployments. Step 1: Create a spring boot application with only the SpringBootApplication and Configuration classes, such as one for REST configuration package scanning, one for JPA etc. Most often this wil...

POV: Prototype injection into Spring singleton, an antipattern. Opinions invited

Today someone asked me, how would Spring behave with respect to object instantiation if a Prototype bean was injected into a Singleton. This took me by surprise and the reaction was "Why in the world would we even do that?". Do we ever have instance level objects in Singletons, that might be prone to change, as that is what prototype beans would be for? No we do not, then why would we do that in Spring. The very intent of Singleton is to embody the logic of our application code, that has no state. It makes sense for Prototype beans, to have Singleton beans injected in them, for doing work. But the other way round, not so much. However, I went through the spring documentation and to my surprise there was a section(3.5.3) dedicated to Prototype beans injected into Singleton beans. And as expected, though it does not fail, such a dependency injection just results in a fixed instance of the Prototype bean to be injected into the Singleton. That instance does not change th...