简体   繁体   English

我如何@Autowire 一个从外部罐子创建的弹簧豆?

[英]How can I @Autowire a spring bean that was created from an external jar?

I have a module/jar that I've created and am using as a util library .我有一个我已经创建并用作 util library的模块/jar。 I created a service in there like so:我在那里创建了一个服务,如下所示:

@Service
public class PermissionsService { ... }

... where this resides in a package here: com.inin.architect.permissions and in my main application, I'm referencing/loading this jar (ie 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?那么,如何将 lib 中的 bean 带入我的应用程序? 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?您是否必须将库设置为完整的 Spring MVC 应用程序才能正常工作? ie do you have to have @Configuration and @ComponentScan setup in the lib as well?即你必须在库中设置@Configuration 和@ComponentScan 吗?

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 配置的原理相同。

Just a note on this, but you could decouple your dependency from spring.只是对此的说明,但您可以将您的依赖与 spring 分离。 In your @Configuration class create在你的@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 之外可用。

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..."但是 - 自动装配接口总是失败,并显示“找不到 blablabla...”

But the interface was in the same package as the beans i could import.但是接口与我可以导入的 bean 位于同一个包中。 It turned out that searching for the mongo db interfaces is NOT taking the scanBasePackages from the @SpringBootApplication into consideration!事实证明,搜索 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.或者您可以将主类“向上”移动,以便默认搜索也适用于 mongo 接口。 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.老实说,我觉得这很愚蠢。

You can import application-context.xml for com.inin.architect.permissions in 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.permissions that you have defined.这将使您能够从您定义的com.inin.architect.permissions自动装配 bean。

I faced the same issue while scanning other classes from other project dependencies, The scanning solution depends on the type of classes you need to scan as follows:我在从其他项目依赖项扫描其他类时遇到了同样的问题,扫描解决方案取决于您需要扫描的类的类型,如下所示:

if they are normal @Component, @Service annotations use如果它们是普通的@Component,@Service 注释使用

@ComponentScan({"com.mypackge1","com.mypackage2"})

If the type of classes are domain objects based on entities use如果类的类型是基于实体的域对象,请使用

@EntityScan("com.mypackge1.domain")

If JPA repository classes如果 JPA 存储库类

@EnableJpaRepositories(basePackages = {"com.mypackage.repository"})

If Redis repository classes use如果 Redis 存储库类使用

@EnableRedisRepositories(basePackages = {"com.mypackage.repository"})

Same for Mongo, etc. Mongo 等也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM