Spring @Autowired constructor does not give default constructor not found

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(); // gives: running... ctx.getBean("testeAutowired"); } } 

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 ...

+4
source share
4 answers

Try using ApplicationContext instead of XmlBeanFactory. XmlBeanFactory does not process postprocess annotations, i.e. it does not use AutowiredAnnotationBeanPostProcessor, which explains the behavior you are experiencing.

Here are some more explanations.

+1
source

Try using

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

This should force Spring to use this constructor. Otherwise, he builds a list of constructors and selects the best candidate. Apparently, he really wants a default constructor as a candidate, I think.

Link: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.html

0
source

Create an interface for the component and try outsourcing the interface and leave the class using the auto-notified constructor.

0
source

I get the same error message but have a different problem. I used the XML configuration and put @Autowired in the class constructor.

I fixed this problem by including annotation-dependent configuration in the XML configuration file:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> 
0
source

Source: https://habr.com/ru/post/1384016/


All Articles