Spring Autowired Constructor Caveats

Here’s a little quiz about Spring autowiring. Given the following classes:

public class RightService {
    //...
}

public class WrongService {
    //...
}

public class MyBean {

    @Autowired
    public MyBean(RightService service) {
        //...
    }
}

Continue reading

Advertisements

Saner Maven Dependencies

Maven’s popularity is partly due to its dependency management feature. It’s easy to add a new JAR to your project and let Maven take care of importing any other JARs required as transitive dependencies.

But this is a double-edged sword. I’ve seen plenty of projects ending up with multiple versions of the same JARs – or multiple versions of the same classes in different JARs – in their classpath. In such cases the Java class loader simply picks classes from the first JAR it finds, with unpredictable results. Most of the time it’s harmless, but occasionally it causes e.g. a NoSuchMethodException at runtime in one environment, despite everything working fine in another environment. Continue reading