Monday, October 17, 2011

OpenSource & RadioShack Myth

I like this quote by Larry (it's a bit old though, but very true)


"One of the myths of open-source is that it's built by a bunch of guys who work at RadioShack (a U.S. electronics store). And when they get home at night, they log on to the Internet and write codes," he said. "The largest investor of Linux with the most number of Linux engineers is from a little RadioShack-related company called IBM."


Read More....





Thursday, October 13, 2011

How to Autowire an argument to a constructor in Spring.

Look into this class:

public class SomeServiceImpl{


    @Autowired
    private AnotherService anotherService;


    public SomeServiceImpl(){
            doThisWithAnotherService();
    }


    private void doThisWithAnotherService(){
            anotherService.doThat();
    }
}


The above code will blowup while initilization with a NullPointerException inside the doThisWithAnotherService().  The reason being the constructor is called before AnotherService bean is injected.

Solution


public class SomeServiceImpl{


    private AnotherService anotherService;


    @Autowired
    public SomeServiceImpl(AnotherService aService){
        this.anotherService = aService;
        doThisWithAnotherService();
    }


    private void doThisWithAnotherService(){
        anotherService.doThat();
    }
}



Fields are injected right after construction of a bean before any config methods are invoked.

Source: http://static.springsource.org/spring/docs/2.5.x