弹簧-另一个项目或罐子中的电线组件 [英] Spring - wire component from another project or jar

查看:41
本文介绍了弹簧-另一个项目或罐子中的电线组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个基本问题,但是我对使用Spring还是很陌生.

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

我在Eclipse中设置了2个Maven项目,都使用Spring.我希望一个项目在另一个项目中使用Spring @Service.我正在尝试通过使用@Resource注释对成员变量进行注释来做到这一点.我在pom.xml中添加了对另一个项目的依赖.

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.

但是,当运行Spring时,却找不到我要在其他项目中使用的类.为了让Spring在另一个项目中找到对象,我还需要做其他事情吗?当所有代码都在一个项目中时,它工作正常,那么我是否需要告诉它以某种方式扫描另一个项目?

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?

我尝试添加@ComponentScan(),但没有帮助.

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

回答人们问我的问题.如果我在Spring上做一些根本错误的事情,或者这是基本的事情,再次道歉.

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.

因此,我有2个Maven项目,都是使用Spring Tool Suite作为Spring Starter项目创建的.但是,这两个项目都不是Web项目,一个是命令行工具,另一个是要由该工具使用的库,可能是我接触到其他项目时所用的库.因此,两者都被创建为JAR文件.

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.

该工具在作为库的项目的pom.xml中具有依赖项.它运行良好,我可以让Maven为其创建JAR文件等.由于它们都是Maven项目,因此它们都具有标准的Maven文件结构,并带有"src/main/java"中的Java代码.

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.

接下来,我目前仅使用Java注释来配置spring,而不是XML.所以我没有XML:

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

<context:annotation:config/>

在任何地方出现.是否需要这种XML来实现我在这里要做的事情?

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

推荐答案

感谢所有帮助,但实际上我发现自己做错了!

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

我正在使用@ComponentScan批注,但未在其他Maven项目中指定包.

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.

这篇关于弹簧-另一个项目或罐子中的电线组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆