独立模块中的spring-boot-starter-parent导致其bean无法在依赖模块中看到

时间:2017-03-18 23:24:22

标签: spring-boot parent-pom

我是一个聚合器POM,它引用了2个模块' appA'和'appB'。 '程序appB'取决于appA'作为依赖 '程序appB'有弹簧启动 - 启动 - 父母'作为父POM '的appA'有一个bean(' hwBean')在包" com.multipom.config"中定义。

当appB'应用程序运行,它看到' hwBean'只有当appA'不使用 '弹簧引导起动父'作为父母POM。 当appA'使用' spring-boot-starter-parent'作为父母POM,' hwBean'在appA'中定义的bean不适用于appB'。这是一个弹簧启动错误吗?

  AppA模块中的

---- com.multipom.App_A.java ------

package com.multipom;

@SpringBootApplication
public class App_A {
    public static void main(String[] args) {
        SpringApplication.run(App_A.class, args);
    }
}

---- com.multipom.config.ApplicationConfig.java

package com.multipom.config;
@Configuration
public class ApplicationConfig {
    @Bean
    public String hwBean () {
        return "Hello World";
    }
}
  

在AppB模块中

---- com.multipom.App_B.java ------

package com.multipom;

@SpringBootApplication
public class App_B {
    public static void main(String[] args) {
        SpringApplication.run(App_B.class, args);
    }
}
  

根/ pom.xml的

<groupId>com.splitpom</groupId>
<artifactId>root</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
    <module>./appA</module>
    <module>./appB</module>
</modules>
  

程序appB / pom.xml的

<groupId>com.multipom</groupId>
<artifactId>appB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>appB</name>
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.2.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
  <dependency>
    <groupId>com.multipom</groupId>
    <artifactId>appA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>
  

appA / pom.xml(案例1)

<groupId>com.multipom</groupId>
<artifactId>appA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>appA</name>
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.2.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
  

appA / pom.xml(案例2)

<groupId>com.multipom</groupId>
<artifactId>appA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>appA</name>
<parent>
  <groupId>com.splitpom</groupId>
  <artifactId>root</artifactId>
  <version>1.0.0</version>
</parent>
$ java -jar appA-0.0.1-SNAPSHOT.jar

2017-03-18 11:23:23.340 TRACE 26149 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appA/target/appA-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/config/ApplicationConfig.class]
2017-03-18 11:23:23.415 DEBUG 26149 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: URL [jar:file:/multipom/appA/target/appA-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/config/ApplicationConfig.class]
2017-03-18 11:23:23.416 TRACE 26149 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appA/target/appA-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_A.class]
2017-03-18 11:23:23.430 TRACE 26149 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: URL [jar:file:/multipom/appA/target/appA-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_A.class]
2017-03-18 11:23:24.084 DEBUG 26149 --- [           main] a.ConfigurationClassBeanDefinitionReader : Registering bean definition for @Bean method com.multipom.config.ApplicationConfig.hwBean()

案例1:如果&#39; appA&#39;使用&#39; spring-boot-starter-parent&#39;作为父母POM(请注意,&#39; appB&#39;没有找到&#39; hwBean&#39;来自&#39; appA&#39;

$ java -jar appB-0.0.1-SNAPSHOT.jar

2017-03-18 11:24:31.461 TRACE 26203 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_B.class]
2017-03-18 11:24:31.546 TRACE 26203 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_B.class]

案例2:如果&#39; appA&#39;不使用&#39; spring-boot-starter-parent&#39;作为父母POM(请注意,&#39; appB&#39;确实找到&#39; hwBean&#39;来自&#39; appA&#39;。

$ java -jar appB-0.0.1-SNAPSHOT.jar   
2017-03-18 12:03:28.261 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_B.class]
2017-03-18 12:03:28.316 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_B.class]
2017-03-18 12:03:28.316 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/App_A.class]
2017-03-18 12:03:28.324 DEBUG 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/App_A.class]
2017-03-18 12:03:28.325 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/config/ApplicationConfig.class]
2017-03-18 12:03:28.330 DEBUG 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/config/ApplicationConfig.class]
2017-03-18 12:03:28.348 DEBUG 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2017-03-18 12:03:28.373 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_B.class]
2017-03-18 12:03:28.384 DEBUG 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/multipom/App_B.class]
2017-03-18 12:03:28.385 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/App_A.class]
2017-03-18 12:03:28.388 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Ignored because not matching any filter: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/App_A.class]
2017-03-18 12:03:28.389 TRACE 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Scanning URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/config/ApplicationConfig.class]
2017-03-18 12:03:28.390 DEBUG 27097 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: URL [jar:file:/multipom/appB/target/appB-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/appA-0.0.1-SNAPSHOT.jar!/com/multipom/config/ApplicationConfig.class]
2017-03-18 12:03:29.123 DEBUG 27097 --- [           main] a.ConfigurationClassBeanDefinitionReader : Registering bean definition for @Bean method com.multipom.config.ApplicationConfig.hwBean()

1 个答案:

答案 0 :(得分:1)

问题在于spring-boot-maven-plugin打包依赖jar的方式。在默认设置中,这个插件将jar打包成一个特殊的可执行jar格式,它将所有类放在/ BOOT-INF / classes / ....如果你的jar用作另一个模块的依赖,你不需要那。相反,你的jar必须是一个普通的扁平jar,这样依赖的应用程序就可以发现所有包含的bean。解决方案是将以下内容添加到spring-boot-maven-plugin部分(在appA / pom.xml中):

    <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </execution>
        </executions>