Curse of the Dependency Injection Framework

Dependency injection is, of course, a useful technique, but it’s also incredibly trivial. Most engineers have been using DI for their entire careers, long before this trivial construct was given a silly name. But then again a morning meeting is now called a “daily scrum”, an open plan office a “bull pen”, a period of work a “sprint” and the annoying guy looking over your shoulder and nit-picking at every line of code you write is not the office psychopath, he’s just “extreme programming”. Welcome to the bizarre world of geek speak. A world in which men (and I use the term loosely) who have spent far too many hours in front of an LED screen and far too few with the opposite sex seek to raise their societal status by assigning ridiculous words to common everyday occurrences. And if all this wasn’t bad enough, now enter the framework: a large, cumbersome configuration system that does, well, basically nothing.

First let’s examine the trivial construct of DI. DI is simply the act of passing one object into another, eg.

class S {
  privte I i;
  public S(I i) {
    this.i = i;
  public String f() {
    return i.f();

I i = new I();
S s = new S(i); //  <- Dependency injection!

Yes, that trivial single line is what all the fuss is about. The basic reason to do this is simply because we are modelling a situation where the host object s does not know how to instantiate i. Typically I may also be an interface such that various different types of I can be passed to s. Also, typically, one implementation may be a real “live” implementation L whilst another may be mock for testing T.

interface I {
  String f();

class L implements I {
  public String f() { 
    return "live"; 

class T implements I {
  public String f() { 
    return "test"; 

S sl = new S(new L());
S st = new S(new T());

So, it’s pretty clear and very trivial that DI can be performed in simple Java. No frameworks or XML config files are needed to achieve this. The service S can be set up for either live purposes or for unit tests with mock dependencies. During a unit test an example setup may be to use the @Before annotation:

private S s;

void before() {
  s = new S(new T());

void test() {

In a live application an example setup may be:

static void main() {
  S s = new S(new L());
  println(s.f()); // Prints "live"

It can be clearly seen that there is absolutely no need for any type of framework whatsoever in order to achieve the required goals.

Suppose s is a servlet running inside a JEE container then in this case the JEE container is responsible for instantiating s. A little more work is therefore required to set up s. A “live” service class LS is derived from S such that it constructs s with the relevant “live” dependencies. LS is then used as the actual servlet and of course S remains testable as before.

class LS extends S {
  public LS() {
    super(new L());

It can therefore be clearly seen that the trivial notion of DI can be easily implemented without the use of any type of framework and simply by using standard and simple Java code. Nevertheless, a great many engineers still insist on using large and cumbersome frameworks such as Spring to perform this trivial task, somehow under the misguided belief that this is the only way to achieve DI. Personally I have yet to meet a single engineer who can properly justify his rational for using Spring and I have never see a piece of DI framework code that cannot be written more simply and more concisely in plain Java.

Spring has indeed become a curse. It is slow and cumbersome, but worst of all it requires code that would have ordinarily been written in Java to be migrated to XML configuration files. It goes without saying that XML is not compiled or type checked as Java is, and therefore it creates an extremely brittle and difficult to follow architecture. It is a truly bizarre state of affairs that almost the entire Java community has resolved to deliberately make life very difficult for themselves in what appears to be a typical case of groupthink, a monolythic body of unthinking developers blindly following the rantings of the Spring officionados.

Eventually Spring will die because in the end reality will finally dawn, but until then the Java world will continue to be cursed by the dependency injection framework.

Curse of the Dependency Injection Framework


Today, Andrew Wakefield, the doctor who instigated the MMR vaccine scare in 1998, was ruled to have acted unethically by the GMC.

It is interesting to note that the main points raised by the GMC (according to BBC news) were primarily concerned with the way in which he had taken blood samples from children. He was also accused of being dishonest for failing to dislose that he was being paid by solicitors employed by parents who belived their children had been victims of the MMR jab. So, no actual science at all then. It is quite clear that the GMC, unable to reason against Mr Wakefield on scientific gounds, instead sought to discredit him with what basically amounts to name calling.

I freely admit that I do not know all the ins and outs of this case, or if MMR causes autism, but one thing I do know is that injecting toxic chemicals into your body is neither a safe, nor intelligent, thing to do.

The graph that has been produced to show that measles cases have increased as a direct result of the lack of uptake of the MMR jab clearly shows nothing of the sort. Between 1996 and 2003, it could be argued that the two variable follow similar trends, with MMR uptake declining slightly and measles increasing slightly, however after 2003 MMR uptake increases again and then stabilises, yet measles cases increase dramatically showing that despite MMR uptake increasing there appears to be no gained protection agains measles.

Furthermore, the uptake of MMR declined in total by only around 4% from around 90% to around 86% whereas measles cases have risen from around 100 cases to around 1,400 cases, approximately 1,400%. It can be clearly seen that the two percentage changes are not related in any way.

The graph really shows no correllation at all between the two variables and is infact much more likely a good indicator that MMR has nothing whatsoever to do with the number of measles cases. Certainly, in my opinion, this is the truth of the matter.

Graph from BBC News website


The Story of Money

Mr Smith and Mr Jones, the sole survivors of a sunken holiday cruise ship, washed ashore a small deserted island in the middle of the ocean. After the initial shock of their ordeal they decided that they ought to get on and start surviving until another cruise liner happened by to pick them up. There was lots to do and Smith and Jones decided that it would be beneficial for them to help each other to build shelters, gather food and catch fish. These type of activities would involve a certain amount of effort on behalf of each person that would ultimately benefit the other. Unfortunately, Smith and Jones had only just met on the cruise and did not completely trust each other and so they decided to construct a system whereby if one helped the other this would be noted down and remembered so that at a later date the work could be repayed.

In order to implement this system Smith and Jones decided to use leaves to represent each IOU. If Smith helped Jones then Jones would give Smith a leaf to represent the fact that he owed Smith some help in return. Smith could then redeem this help at a later date by swapping it for the leaf. Furthermore, they decided that they would not cheat and pick extra leaves when they shouldn’t do. Depending on the amount of help one gave the other they determined the value of this help in terms of leaves. So, if one helped the other build a shelter they agreed that this was worth 10 leaves, or if one helped the other pick a coconut this was worth 1 leaf, and so on.

Smith and Jones started with no leaves each. Smith asked Jones for help building his shelter at a cost of 10 leaves. But how was Smith to pay Jones if he didn’t have any leaves to begin with? They came up with the following solution: They agreed that Smith could take 10 leaves from the tree, but when he did this he had to make 10 marks on the tree with some charcoal. These 10 marks would represent the fact that Smith had borrowed 10 leaves from the tree temporarily and did not own them, rather he had to give them back to the tree at some later date.

Smith then gave his 10 borrowed leaves to Jones and Jones helped him build his shelter. The next day Jones decide to redeem Smith’s debt to him and asked Smith if he would help him catch some fish. Smith helped Jones catch 10 fish at a cost of 1 leaf per fish, resulting in all the leaves being returned to Smith.

Smith could have kept the 10 leaves in his possession, but why carry around a load of leaves all the time? It had already been noted on the tree, with charcoal marks, that Smith had only borrowed the 10 leaves so instead of carrying them around he returned them to the tree and erased the marks, clearing his debt.

This system worked perfectly for weeks. Smith and Jones’ micro-economy allowed them to trade in goods and services that enabled them to help one another and resulted in a well functioning camp and society on the island. Smith and Jones mused that they were glad that they were stranded and would not like to return back to working in their city banking jobs anymore.

That was until Mr Brown arrived.

Fresh from another stricken holiday cruise ship, Mr Brown came up from the sea spluttering and choking, and Smith and Jones welcomed him onto the island. Eventually they explained the micro economy that they were using and showed Brown how it had allowed them to survive on the island.

Mr Brown, however, had seen all this before and said “That’s all very nice, you’ve done a great job there. But what you haven’t considered is inflation and market forces. What will happen when the price of a coconut doubles, or the demand for fish declines? You could end up walking around with thousands of leaves in your pockets!”

Smith and Jones looked puzzled.

“What you need, is stability” said Brown. “I’ve been in the banking business all my life and I know how this stuff works. What you need is an organization to manage and control the supply of leaves. This will help you control those boom and bust cycles when the amount of leaves in circulation fluctuates over time.”

Smith looked at Jones. Jones looked at Smith.

“What you need, is a central bank” said Brown. “Without a central bank your economy could easily collapse and you will both be destitute! Now, luckily for you folk, I can help you out because I know all about central banks. I’ll set one up and run it for you, how about that?”

Smith and Jones were now starting to get nervous. They didn’t really understand all this economic mumbo jumbo, but it did seem that at any time their entire economy could collapse and leave them destitute. They were very lucky, they thought, that Brown had come along at such a timely moment, and wasted no time in installing Brown as the manager of their new “Central Bank”. Smith and Jones went back to lie in the shade of their shelters and felt safe in the fact that their economy was now stable.

Brown got to work immediately and started by building a big fence around the tree because he didn’t want Smith and Jones getting any funny ideas. Then he decided that the economy should start from scratch, so he implemented a program called The Island Leaf Seizure. Smith and Jones had to give up any leaves they currently owned, to Brown. This was for the good of the economy, Brown assured them. Brown pondered what he would do with his new leaves. A shelter perhaps? Or maybe some coconuts…?

Soon the time came that Smith wanted to borrow some leaves from the Central Bank so he asked Brown for a loan. Brown handed out 10 leaves to Smith, and carefully noted down in charcoal marks that Smith owed him 11 leaves in return. This puzzled Smith, but he assumed Brown knew what he was doing.

The leaves circulated between Smith and Jones in accordance with their services to each other and eventually Smith decided to pay off his loan to the bank and returned the 10 leaves. On receiving the leaves, Brown cleared his throat and looked intently at Smith, then said “I believe you owe me 11, not 10 leaves.”

“But I only have 10 leaves” said Smith.

“Hmm. I see.” muttered Brown. “Well not to worry, you can defer your payment for a while, but what I suggest is this: Next time you come to borrow some money, make sure you borrow a bit extra so that you can pay of this extra leaf that you owe me. Sound good to you?”

Smith had to admit that he had been puzzled as to how he would pay off this extra leaf, considering that nobody in the economy had any more leaves, but how he understood how it all worked. “That’s fine” he agreed.

So Smith and Jones followed this pattern for a while. Next time Smith went to borrow some leaves he borrowed 11 so that he would have 10 spare and could pay off his debt of 1. The problem was, however, that now Smith owed 12 but only had 10. “Don’t worry” Brown assured them. “Just keep on borrowing.”

So Smith and Jones borrowed more and more and fell further and further into Brown’s debt. During this time Brown had accumulated quite a few leaves in interest and had employed Smith and Jones to build an impregnable fence around the tree, a fancy shelter for him to live in (much nicer than their own they noted) and currently had them employed in digging out a pool for to keep him cool during the long sunny days.

Eventually Smith and Jones had run up very large debts and they went to Brown to borrow even more. Upon being asked for another loan, Brown replied “Well, we have a slight problem here. You see the thing is, the bank is having to tighten up its’ lending practices. Too many bad debts you see. I’m sorry, but I’m going to have to start calling in these loans, including the interest of course, and I won’t be issuing any more for a while either.”

Smith and Jones looked worried. “Ah” said Brown. “Don’t worry. This is just the business cycle. Happens all the time. This is the point of a central bank you see, to regulate the amount of leaves in the market, make sure we control the boom that we’ve been having.”

Brown hauled himself out of his newly built pool, flopped into an impressively carved wooden armchair and gestured to the luxury villa that Smith and Jones had built for him. “See what our economy has brought us?” he said. “This is capitalism at it’s finest. We have achieved exquisite carvings, innovative architecture and…” he took a slurp from a coconut cocktail “…fine food and drink”. Brown relaxed back into his chair. “Without our economy neither of you two would have had jobs. You’d have been sitting around all day, but look what you have achieved now!” He took a long puff on an exceptionally fat banana cigar that Jones had rolled for him earlier that day, smiled and closed his eyes.

Smith and Jones were glad that the boom was under control. Too much boom was a bad thing Brown had warned them, so they paid off their loans as best the could but they couldn’t pay off the interest, because there was no more money in the economy.

“Well” said Brown. “We do have a problem now don’t we? You see we always get this. Bad debters. People just think they can go on borrowing money and spending it on this and that without a thought as to how they can repay it. You two should be ashamed of yourselves!” Smith and Jones looked sheepish. “Alright, look you’ll have to give me whatever you have and that’ll have to do.” Brown got out a new parchment notepad and started to note down his new assets. “Two shelters…, 4 fish, 3 coconuts… is that it? Alright hand it over then.”

Smith and Jones handed over everything they had to Brown and then walked down the beach to the water’s edge. They sat there in silence with the cool sea lapping at their toes as they gazed out towards the setting sun. The faint aroma of a banana cigar hung on the gentle island breeze.

The Story of Money

A Simple API for Parsing XML in Java

You can do a lot with XML, but often all you want to do is to read a simple file with some basic data in it. The options for doing this, SAX, DOM and JAXB are all relatively verbose and often off-putting. I’ve devised a simple XML API for parsing simple XML files in Java. Parsing a file such as:

		<role name="admin"/>
		<role name="user"/>
		<user name="joe" password="pass" role="admin"/>
		<user name="harry" password="secret" role="user" email=""/>

Can be achieved with the following code:

Xml config = new Xml("config.xml","config"); 
System.out.println("title: "+config.child("title").content());

Xml version = config.child("version");
System.out.println("version: "+version.integer("major")+"."+version.integer("minor"));

for(Xml role:config.child("roles").children("role")) 
	System.out.println("role: name: "+role.string("name"));

for(Xml user:config.child("users").children("user")) {
	String email = user.optString("email");
		"user: name: "+user.string("name")+
		", password: "+user.string("password")+
		", role: "+user.string("role")+
		", email: "+(email==null ? "-" : email));

System.out.println("test: "+config.option("test"));


title: test
version: 1.2
role: name: admin
role: name: user
user: name: joe, password: pass, role: admin, email: -
user: name: harry, password: secret, role: user, email:
test: true

As you can see, it’s nice and simple and allows you to get to the information quickly and without any hassle. The API uses the DOM parser underneath, but attempts to make the data more easily available. All you need is this class, which you can of course customise however you like.

A Simple API for Parsing XML in Java