Java 嵌套 jar 的 Spring 组件扫描

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20086464/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:01:19  来源:igfitidea点击:

Spring component scan for nested jar

javaspringjarautowiredapplicationcontext

提问by mel3kings

Basically I have this Outer.jar, declared in it is an application context with a component scan:

基本上我有这个 Outer.jar,在其中声明是一个带有组件扫描的应用程序上下文:

<context:component-scan
        base-package="x.y.z.class" />

However this x.y.z.classis in an inner.jar which is a dependency of outer.jar,

然而,这x.y.z.class是在一个inner.jar中,它是outer.jar的一个依赖项,

I'm getting an error that class not found .../Outer.jar/x/y/z/class, how can specify to check in the inner.jar?

我收到一个错误,找不到类.../Outer.jar/x/y/z/class,如何指定检查inner.jar?

UPDATE: Initialize Application context as:

更新:将应用程序上下文初始化为:

 org.springframework.context.ApplicationContext ctx = 
                 new ClassPathXmlApplicationContext("applicationContext.xml");

exception: I/O failure during classpath scanning; nested exception is java.io.FileNotFoundException: ..\default\deploy\test.war\WEB-INF\lib\inner.jar\x\y\z
 and it says inner.jar/x/y/z/class not found

Outer.jar has inner.jar as the dependency

Outer.jar 有 inner.jar 作为依赖

采纳答案by gerrytan

Make sure inner.jar (or whatever you name it to) exist on your classpath (either by adding to maven dependency, eclipse project settings, using -cp jvm command line arguments, etc), and just refer to the package name of the classes inside inner.jar you want to include.

确保您的类路径中存在 inner.jar(或您命名的任何名称)(通过添加到 maven 依赖项、eclipse 项目设置、使用 -cp jvm 命令行参数等),并且只引用类的包名称在您想要包含的inner.jar 中。

Also make sure you don't get confused between jar, base package and fully-qualified class name. If I have a class com.mycoolcompany.service.Booyainside Blah.jar, typically I just need to do

还要确保您不会在 jar、基本包和完全限定的类名之间混淆。如果我com.mycoolcompany.service.Booya在 Blah.jar 中有一个类,通常我只需要做

<context:component-scan base-package="com.mycoolcompany.service.*" />

And ensure Blah.jar is on the parent project's classpath

并确保 Blah.jar 在父项目的类路径上

回答by Anuj Patel

If you are sure that the jar is under your classpath and still your classs is not autowired.

如果您确定 jar 在您的类路径下,并且您的类仍然没有自动装配。

Are you sure that this classes are annotated properly?? Because what happens when you say component-scan, spring tries to find classes annotated with @Componentor subtypes of compnenets like @Service, etc.

你确定这个类被正确注释了吗?因为当你说的时候会发生什么component-scan,spring 试图找到用@Component诸如@Service等的comnenets注释的类或子类型。

My guess is this is your third party dependency which might not have annotated classes. In this case you should define beansmanually in application context. HTH

我的猜测是这是您的第三方依赖项,它可能没有带注释的类。在这种情况下,您应该beans在应用程序上下文中手动定义。HTH