Yes, you heard it here first folks, but Sun has finally woken up and decided to include a micro IoC container in the next version of Java! Using a special keyword, the container lets you create an object that can be used straight away, by passing all dependencies in.
So where before with Spring Id have to write my configuration file:
1
Or with Pico, where I have to kiss a boy:
public class Boy {
public void kiss(Object kisser) {
System.out.println("I was kissed by " + kisser);
}
}
public class Girl {
Boy boy;
public Girl(Boy boy) {
this.boy = boy;
}
public void kissSomeone() {
boy.kiss(this);
}
}
And create and use them like this:
pico.registerComponentImplementation(Boy.class);
pico.registerComponentImplementation(Girl.class);
Girl girl = (Girl) pico.getComponentInstance(Girl.class);
girl.kissSomeone();
With the brand new micro IoC container I can do this instead!
Object myObject = new Object(somethingINeedToWork);
W00t!
Update: Wow! It appears that the IoC micro container has been in Java for years! And here I am, up to my arse in code I cant debug with XML configuration files its hard to test! Dont I feel stupid. Id best go off and write an article for the ServerSide on this ASAP
9 Responses to “Micro-IoC functionality to be in next Java version”
Hey, where’s the Object(Object) constructor? I looked at the Javadocs for Java 5, and there was nothing there. Is can’t be some clever Mustang trick, cause you mention that’s been around for years… are you sure you’re not using J# or some other implementation of Java? 🙂
What a pity, though. That constructor would be so nice. Object being its own wrapper class is the way to go for all the Java devs who feel their code is not yet running on too many layers of barely-hidden (abstracted?) complexity.
Sam has added the Object(Object) ctor with Annotations, AOP and RoR. It’s very nice for Ajax.
I bet if we could implement this new paradigm (as in paradignism) into Ruby, it would be even better, and would automatically make the program, and everyone associated with it, better too!
Despite the lack of an Object(Object) constructor in real life, this blog entry rulez.
Maybe there is an Object(Object) constructor anyway. How should I know. I’m Not A Java Programmer.
Also, forcing me to give you my email address to post here is STUPID.
Forcing email is an (outmoded) form of spam prevention – mostly it’s so I can write people back if they said something interesting/cyber-stalk you if you insulted me – as the form says though, I don’t display it…
I guess Sam’s object is one of these:
package com.magpiebrain;
public class Object {
public Object(Object somethingINeedToWork) { … }
public void makePoint() { }
}
BTW Sam, ta for drawing my attention to that. I posted the comment I really wanted to write here on my blog ‘cos it was too long.
Too much coffee today? :-p
Cool 🙂 wish I had figured this out at the beginning of my current project.
Ah, good old constructors. You’re quite right, they are a form of inversion of control (look ma, no XML). Unfortunately, just about everyone seemed to have forgotten them and tended to write code that looked more like this:
public class SomeObject {
public void doSomething() {
Context.lookup(“service”).doSomething();
}
}
which is not nearly as nice.