Search code examples
hibernatespring-bootentity

Pass DB Entity to another class gives, could not autowire error


Not sure I understand why I am getting compilation error on code below. I am looping a list DB Entities and if it does not exists in my main list then I create a new Camel Route and pass the entity as a Parameter to another class.

@Component
public class MailRouteProcess implements Processor {

    private static List<MailDataSource> mailDSList = new ArrayList<>();

    @Autowired
    MailDataSourceService mailDataSourceService;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    CamelContext camelContext;

    @Override
    public void process(Exchange exchange) throws Exception {
        List<MailDataSource> mailDataSourceList = mailDataSourceService.findAll();
        for(MailDataSource mailDataSource : mailDataSourceList) {
            if(!mailDSList.contains(mailDataSource)) {
                camelContext=(CamelContext)applicationContext.getBean("mainCamelContext");
                camelContext.addRoutes(new MailRouteBuilder(camelContext, mailDataSource));
                mailDSList.add(mailDataSource);
            }
        }
    }
}

Then in my MailRouteBuilder class I have:

@Component
public class MailRouteBuilder extends RouteBuilder {

    public MailRouteBuilder(CamelContext camelContext, MailDataSource mailDataSource) {
        super(camelContext);
    }

But getting the following compilation error on the MailDataSource mailDataSource:

Could not autowire. No Beans of 'MailDataSource' type found.

Any ideas?


Solution

  • You are trying to instantiate your MailRouteBuilder in two different ways:

    • In MailRouteProcess.exchange you call the Constructor manually, and pass MailDataSource obtained from MailDataSourceService.
    • On the other hand, you annotated MailRouteBuilder as a @Component, so Spring will try to instantiate it. In this case MailDataSource must be a managed bean. Judging from your error, it isnt.

    To sum it up, the error stems not from passing an instance to another and one, but from failed autowiring. (Do you need your MailRouteBuilder to be a managed component?)