Spring @Autowired constructor gives No default constructor found

Some strange behavior from Spring 3.0 here.

package com.service.schedule;

import org.springframework.stereotype.Component;

@Component("outroJob")
public class OutroJob {

    public void printMe() {
        System.out.println("running...");
    }

}

and

package com.service.schedule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Component("testeAutowired")
public class TesteAutowired {

    @Autowired
    public TesteAutowired(OutroJob outroJob) {
        outroJob.printMe();
    }

    public static void main(String[] args) {
        ClassPathResource res = new ClassPathResource("applicationContext.xml");
        XmlBeanFactory ctx = new XmlBeanFactory(res);

        OutroJob outroJob = (OutroJob) ctx.getBean("outroJob");
        outroJob.printMe(); // gives: running...

        ctx.getBean("testeAutowired");
    }
}

None of these beans are declared on applicationContext.xml

So, line outroJob.printMe(); works fine... prints "running..."

But when I try to get the "testeAutowired" bean, it says:

Could not instantiate bean class [com.service.schedule.TesteAutowired]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.service.schedule.TesteAutowired.

The question is: why, if Spring found the "outroJob" bean it doesn't autowired it on the TesteAutowired constructor ?

It seems obvious what it has to do...

Answers


Try using ApplicationContext instead of XmlBeanFactory. XmlBeanFactory doesn't postprocess annotations ie doesn't use AutowiredAnnotationBeanPostProcessor which would explain the behaviour you're experiencing.

Here's some more explanation


Need Your Help

Silverlight: Determine number of items based on ListBox width

c# silverlight listbox

There is a horizontal ListBox (of Images) in my application. At application startup, I need to add some Images to the ListBox so that it fills the window width (no scroll is allowed). These images...

Downloading a file from url and saving to resources on iPhone

ios cocoa-touch nsurlconnection

Is it possible to download a file (i.e. an sqlite database file) from the Internet into your iPhone application programmatically for later use within the application?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.