Java 我如何@Autowire 一个从外部罐子创建的弹簧豆?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29571304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I @Autowire a spring bean that was created from an external jar?
提问by BrianP
I have a module/jar that I've created and am using as a util library. I created a service in there like so:
我有一个我已经创建并用作 util library的模块/jar 。我在那里创建了一个服务,如下所示:
@Service
public class PermissionsService { ... }
... where this resides in a package here: com.inin.architect.permissionsand in my main application, I'm referencing/loading this jar (i.e. set as a dependency in the maven POM.xml file for the app) like so:
...它驻留在此处的包中:com.inin.architect.permissions和在我的主应用程序中,我正在引用/加载这个 jar(即设置为应用程序的 maven POM.xml 文件中的依赖项),例如所以:
<dependency>
<groupId>com.inin.architect</groupId>
<artifactId>permissions</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
and within the application I want to use that service like:
在应用程序中,我想使用该服务,例如:
@Autowired
PermissionsService permissions
In the application's spring setup, I've got this:
在应用程序的 spring 设置中,我有这个:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.inin.generator", "com.inin.architect.permissions" })
public class WebConfig extends WebMvcConfigurerAdapter implements ServletContextAware { }
However when I run my application under tomcat, it complains that there isn't a bean for the PermissionsService: "org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ..."
但是,当我在 tomcat 下运行我的应用程序时,它抱怨没有PermissionsService的 bean :“org.springframework.beans.factory.NoSuchBeanDefinitionException: Noqualifying bean of type ...”
So, how can I bring over the bean from the lib into my application? Surely there's a way. Do you have to set the library up as a full blown spring MVC application so that this can work? i.e. do you have to have @Configuration and @ComponentScan setup in the lib as well?
那么,如何将 lib 中的 bean 带入我的应用程序?肯定有办法。您是否必须将库设置为完整的 Spring MVC 应用程序才能正常工作?即你必须在库中设置@Configuration 和@ComponentScan 吗?
采纳答案by Benjamin Boutier
You have to scan at least the package containing the class you want to inject. For example, with Spring 4 annotation:
您必须至少扫描包含要注入的类的包。例如,使用 Spring 4 注释:
@Configuration
@ComponentScan("com.package.where.my.class.is")
class Config {
...
}
It is the same principle for XML configuration.
XML 配置的原理相同。
回答by Maleck13
Just a note on this, but you could decouple your dependency from spring. In your @Configuration
class create
只是对此的说明,但您可以将您的依赖与 spring 分离。在你的@Configuration
课堂上创建
@Bean public PermissionsService permissionsService(){
return new PermissionsService()
}
This will also allow it to be injected. Not that you have to remove your spring annotation, just an option making it potentially usable outside of spring.
这也将允许它被注入。并不是说您必须删除 spring 注释,只是一个选项,使其可能在 spring 之外可用。
回答by s91g
You can import application-context.xml for com.inin.architect.permissionsin the following manner inside your main application.
您可以在主应用程序中按以下方式为com.inin.architect.permissions导入 application-context.xml 。
<import resource="classpath:/permissionApplicationContext.xml" />
This will enable you to autowire beans from com.inin.architect.permissionsthat you have defined.
这将使您能够从您定义的com.inin.architect.permissions自动装配 bean 。
回答by Holgi P
Ok - i had exactly the same problem - i wanted to autowire a mongo db repository interface from an external jar.
好的-我遇到了完全相同的问题-我想从外部 jar 自动装配 mongo db 存储库接口。
I could autowire every bean from that jar with using
@SpringBootApplication(scanBasePackages = {"com.myrootpackage"})
However - autowiring the interface always failed with "Could not find blablabla..."
我可以自动装配那个罐子里的每个豆子
@SpringBootApplication(scanBasePackages = {"com.myrootpackage"})
但是 - 自动装配接口总是失败,并显示“找不到 blablabla...”
But the interface was in the same package as the beans i could import. It turned out that searching for the mongo db interfaces is NOT taking the scanBasePackages from the @SpringBootApplication into consideration!
但是接口与我可以导入的 bean 位于同一个包中。 事实证明,搜索 mongo db 接口并没有考虑来自@SpringBootApplication 的 scanBasePackages!
It has to be explicitly configured via
它必须通过显式配置
@EnableMongoRepositories(basePackages = {"com.myrootpackage"})
Or you could move the main class "up" so the default searching works also for the mongo interfaces. So i understood the problem and found a solution. But i am still a bit unhappy because i need to configure the same lookup path twice. I find it stupid honestly.
或者您可以将主类“向上”移动,以便默认搜索也适用于 mongo 接口。所以我理解了这个问题并找到了解决方案。但是我还是有点不高兴,因为我需要配置相同的查找路径两次。老实说,我觉得这很愚蠢。