magpiebrain

Sam Newman's site, a Consultant at ThoughtWorks

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 I’d 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 can’t debug with XML configuration files it’s hard to test! Don’t I feel stupid. I’d best go off and write an article for the ServerSide on this ASAP…

Advertisements

9 Responses to “Micro-IoC functionality to be in next Java version”

  1. Carlos Villela

    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.

    Reply
  2. Sam Newman

    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!

    Reply
  3. Dustin Aleksiuk

    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.

    Reply
  4. Sam Newman

    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…

    Reply
  5. Liz

    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.

    Reply
  6. Cenk Civici

    Cool 🙂 wish I had figured this out at the beginning of my current project.

    Reply
  7. fletch

    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.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

%d bloggers like this: