Sunday, September 19, 2010

Java IoC containers and classpath scanning (or what I’ve been looking for from .NET for months)

Frustrated with the typical way I saw IoC used in Java where every example I found involved thousands of lines of XML
and/or Java code to configure Java beans or components.  This is very different than IoC typically used in .NET where most IoC containers allow
you to "autowire" in their terminology up every class in an assembly with a couple of lines of code.  Having been coding in that fashion
for several years in .NET I was dismayed when none of my fellow Java coders that I worked with or knew personally had any concept
of the equivalent functionality, and instead informed me the IDE would be my help in maintaining these massive XML files.

Not content with their answers I burrowed into Spring, Guice and PicoContainer docs and found "classpath scanning" which is roughly
equivalent to "autowire" in .NET. Below is an example of this in Spring 3: (my main class note his has 2 dependencies which you can view on my github repo)


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service //one way of marking this as a component to register

public class MyStuff {

    private final DependencyWithNoInterface first;

    private final DependencyInterface second;

    @Autowired //tells spring which constructor to use for it's dependencies

    public MyStuff(DependencyWithNoInterface first, DependencyInterface second) {

        this.first = first;

        this.second = second;


    public void run(){

        System.out.println("foo me");;




appContext.xml (seems you still need some XML)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""







     <context:component-scan base-package=""/>

</beans> (the initialization)


import org.springframework.context.ApplicationContext;


public class Application {

    public static void main(String args[]){

        ApplicationContext ctx =   new ClassPathXmlApplicationContext("appContext.xml"); //the IoC container

        MyStuff stff = ctx.getBean(MyStuff.class); //my fully injected class;




Now I’m sure for the .NET developers familiar with StructureMap, Windsor, Autofac, etc this is completely unimpressive. I’m also sure there are Java developers that are somewhat unimpressed with this but for reasons that involve holding onto giant configuration artifacts. Manual IoC component registration falls where hand writing SQL for trivial data access does for me, extra repetitive work that has been solved years ago. I worked in factories a decade ago, and therefore now have a low tolerance for similar activity as a professional.


It appears you can forgo XML as well in your main class use a different Application context and add the refresh and scan lines


import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {

    public static void main(String args[]){

        AnnotationConfigApplicationContext ctx =   new AnnotationConfigApplicationContext(); 

        ctx.scan(""); //scans the package

        ctx.refresh(); //needed to load them for some reason

        MyStuff stff = ctx.getBean(MyStuff.class);;