在构建jar并运行该jar时,@ ComponentScan for外部依赖程序包在2.1.x中不起作用

时间:2019-04-23 04:59:10

标签: java spring-boot

我在使用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在列表中。

2 个答案:

答案 0 :(得分:1)

很难说出问题的确切原因。 首先,您可以像这样使用ComponentScan

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

请参见Spring Framework ComponentScan

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

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

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

尝试一下。如果此方法有效,至少可以提示您base.jar未正确加载。

答案 1 :(得分:0)

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

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