Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - wire component from another project or jar

Apologies if this is a basic question, but I'm quite new to using Spring.

I have 2 maven projects set up in Eclipse, both using Spring. I would like one project to use a Spring @Service in the other project. And I am attempting to do this by annotating a member variable with the @Resource annotation. I have added a dependency on the other project in the pom.xml.

However when running this Spring is not finding the class I want to use in the other project. Is there something else I need to do in order for Spring to find objects in the other project? It works fine when all the code is in one project, so do I need to tell it to scan the other project somehow?

I've tried adding a @ComponentScan() but that hasn't helped.

EDIT: To answer the questions that people have asked of my problem. Apologies again if I am doing something fundamentally wrong with Spring or if this is basic stuff.

So, I have 2 Maven projects, both created using the Spring Tool Suite as Spring Starter Projects. Neither are web projects though, one is to be command line tool and the other is a library to be used by that tool and probably other projects when I get to them. Therefore both are being created as JAR files.

The tool has a dependency in the pom.xml on the project that is the library. It builds fine, I can get Maven to create a JAR file for it etc. As they are both Maven projects they both have the standard Maven file structure with the Java code I am after in a package in "src/main/java".

The tool is run using:

SpringApplication.run(Application.class, args);

and this works fine when all the classes I am wiring together are in the same project. Now I have moved some of these classes out in to another project it no longer finds them.

Next, I am currently only using Java annotations to configure spring, not XML. So I don't have the XML:

<context:annotation:config/>

present anywhere. Is this XML needed to achieve what I want to do here?

Finally, here is the stack trace I am getting:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.spormeon.adysis.openxdataretriever.services.OpenXDataHandler com.spormeon.adysis.openxdataretriever.Application.openxDataRetriever; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openXDataHandler': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spormeon.adysis.aws.dynamodb.loaders.InventoryReportLoader] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.spormeon.adysis.openxdataretriever.Application.main(Application.java:21)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.spormeon.adysis.openxdataretriever.services.OpenXDataHandler com.spormeon.adysis.openxdataretriever.Application.openxDataRetriever; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openXDataHandler': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spormeon.adysis.aws.dynamodb.loaders.InventoryReportLoader] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openXDataHandler': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spormeon.adysis.aws.dynamodb.loaders.InventoryReportLoader] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 17 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spormeon.adysis.aws.dynamodb.loaders.InventoryReportLoader] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:457)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:435)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)
... 28 more
like image 513
eebsie Avatar asked Mar 09 '15 14:03

eebsie


2 Answers

Thanks for all the help but I have actually discovered what I was doing wrong!

I was using the @ComponentScan annotation, but not specifying the package in the other Maven project.

Adding that in gets everything working as I require.

like image 91
eebsie Avatar answered Nov 04 '22 00:11

eebsie


If this a web project (WAR or EAR) then make sure the dependencies are present in the WEB-INF/lib directory. Also post your application config. Do you have the following present in there.

<context:annotation:config/>
like image 39
zoostar Avatar answered Nov 04 '22 01:11

zoostar