1

我在使用 spring boot 2.1.x.RELEASE 时遇到了问题,因为它没有在组件扫描中扫描基本包,但它在 2.0.x 中运行良好。

@ComponentScan(basePackages = { "com.app.base*", "com.app.child.*" })

使用创建构建时

mvn clean package

它的 jar 已经创建,但是在运行 jar 时它没有从基本外部存储库加载 bean。

java -jar child.jar

例外:

ConfigServletWebServerApplicationContext : Exception encountered during context initialization 
- cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: 
Failed to parse configuration class 

[com.app.child.UserApiApplication]; 嵌套异常是 java.io.FileNotFoundException:类路径资源 [com/app/base/service/BaseService.class] 无法打开,因为它不存在

我在子项目的 lib/base.jar 中添加 base.jar。以下是 pom.xml

<dependency>
  <groupId>com.app</groupId>
  <artifactId>base</artifactId>
  <version>0.2</version>
</dependency>
<repository>
  <id>myRepo</id>
  <url>file://${basedir}/lib</url>
</repository>

将 base.jar 部署到我的本地存储库中

 mvn deploy:deploy-file -DgroupId=com.app -DartifactId=base-Dversion=0.2 -Durl=file:./myRepo -DrepositoryId=myRepo -DupdateReleaseInfo=true -Dfile=lib/base-0.2.jar

当我从 STS 运行应用程序时,组件扫描运行良好。但是当我创建构建并尝试运行 jar 时,它会抛出错误。

我还使用爆炸了 child.jar

jar -tf child.jar

base.jar 在列表中。

4

2 回答 2

2

很难说到底是什么问题。首先,您可以像这样使用 ComponentScan

@ComponentScan(basePackages = { "com.app.base", "com.app.child" })

请参阅Spring 框架组件扫描

在它旁边,您似乎正在尝试运行一个不是胖 jar 的 java 文件,或者您没有将预期的类设置到您的类路径中。

您可以通过在调用中添加类路径来添加此类。

java -cp <path/to/base.jar> -jar child.jar

试试看。如果这有效,至少您有一个提示,即您的 base.jar 未正确加载。

于 2019-04-23T07:33:45.583 回答
0

从 2.1.x 开始,重新打包执行有一个标识符。我们正在添加第二个重新包装,所以最终有两个胖罐子。

                <executions>
                    <execution><!-- 
                        <goals>
                            <goal>repackage</goal>
                        </goals> -->
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>

于 2019-04-26T17:00:09.870 回答