Some weird behavior from Spring 3.0 is 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();
None of these beans are declared in applicationContext.xml
So the line is outroJob.printMe (); works great ... prints "run ..."
But when I try to get a "testeAutowired" bean, it says:
Failed to instantiate bean class [com.service.schedule.TesteAutowired]: default constructor not found; The nested exception is java.lang.NoSuchMethodException: com.service.schedule.TesteAutowired.
Question: why, if Spring found an "outroJob" bean, did it not auto-increment it in the TesteAutowired constructor?
It seems obvious what he should do ...
source share