当前位置: 首页 > 知识库问答 >
问题:

Maven多个具有Spring数据的模块JPA/Spring Boot/Log4j2

宗政博文
2023-03-14

我将spring项目和spring数据JPA拆分为多个模块

>

  • 项目-根-文件夹

    • 项目数据
      • src/main/java/project。测试/应用程序。爪哇
      • src/main/resources/log4j2。xml
      • 波姆。xml
      • src/main/java/project.test/App.java
      • src/main/ressource/log4j2.xml
      • pom.xml

      项目根文件夹/pom。xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>project.test</groupId>
          <artifactId>testapp</artifactId>
          <version>0.0.1</version>
          <name>testapp </name>
          <description>...</description>
      
          <modules>
              <module>project-data</module>
              <module>project-ui</module>
          </modules>
      
          <packaging>pom</packaging>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.2.5.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <java.version>13</java.version>
              <maven.compiler.source>${java.version}</maven.compiler.source>
              <maven.compiler.target>${java.version}</maven.compiler.target>
          </properties>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      项目根文件夹/project data/pom。xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <groupId>project.test</groupId>
              <artifactId>testapp</artifactId>
              <version>0.0.1</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>project-data</artifactId>
      
          <properties>
              <spring-boot.repackage.skip>false</spring-boot.repackage.skip>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-jpa</artifactId>
                  <exclusions>
                      <exclusion>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-logging</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-log4j2</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
                  <exclusions>
                      <exclusion>
                          <groupId>org.junit.vintage</groupId>
                          <artifactId>junit-vintage-engine</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <executions>
                          <execution>
                              <goals>
                                  <goal>repackage</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      项目pom.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <groupId>project.test</groupId>
              <artifactId>testapp</artifactId>
              <version>0.0.1</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>project-ui</artifactId>
      
          <dependencies>
              <dependency>
                  <groupId>project.test</groupId>
                  <artifactId>project-data</artifactId>
                  <version>0.0.1</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
                  <exclusions>
                      <exclusion>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-logging</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
      
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-fxml</artifactId>
                  <version>11.0.2</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-controls</artifactId>
                  <version>11.0.2</version>
              </dependency>
              <dependency>
                  <groupId>net.rgielen</groupId>
                  <artifactId>javafx-weaver-spring-boot-starter</artifactId>
                  <version>1.3.0</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-configuration-processor</artifactId>
                  <optional>true</optional>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
                  <exclusions>
                      <exclusion>
                          <groupId>org.junit.vintage</groupId>
                          <artifactId>junit-vintage-engine</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <executions>
                          <execution>
                              <goals>
                                  <goal>repackage</goal>
                              </goals>
                          </execution>
                          <execution>
                              <id>build-info</id>
                              <goals>
                                  <goal>build-info</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      我想运行项目数据/应用程序。java导致了

      SLF4J: Class path contains multiple SLF4J bindings.
      SLF4J: Found binding in [jar:file:/C:/Users/kk/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
      SLF4J: Found binding in [jar:file:/C:/Users/kk/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
      SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
      SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
      Exception in thread "main" java.lang.ExceptionInInitializerError
      Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
          at org.apache.logging.slf4j.Log4jLoggerFactory.validateContext(Log4jLoggerFactory.java:49)
          at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:39)
          at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:30)
          at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:54)
          at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30)
          at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363)
          at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388)
          at project.test.App.<clinit>(App.java:11)
      

      一个干净的解决方案会很好。谢谢你。

      新生成的单个项目中的信息,pom与该项目根文件夹/project data/pom类似。在上面的xml中,该项目运行良好。。。

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.2.5.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <groupId>com.example</groupId>
          <artifactId>demo</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>demo</name>
          <description>Demo project for Spring Boot</description>
      
          <properties>
              <java.version>11</java.version>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
                  <exclusions>
                      <exclusion>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-logging</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-log4j2</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.getsentry.raven</groupId>
                  <artifactId>raven-log4j2</artifactId>
                  <version>7.8.0</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
                  <exclusions>
                      <exclusion>
                          <groupId>org.junit.vintage</groupId>
                          <artifactId>junit-vintage-engine</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <dependency>
                  <groupId>org.jeasy</groupId>
                  <artifactId>easy-random-core</artifactId>
                  <scope>test</scope>
                  <version>4.1.0</version>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <executions>
                          <execution>
                              <goals>
                                  <goal>repackage</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      </project>
      
  • 共有1个答案

    米树
    2023-03-14

    类路径上的记录器有多个依赖项。一个(或多个)需要在POM文件中排除。对于一些Spring Boot初学者来说,您的示例中已经出现了这种情况。

    要检查类路径上有哪些依赖项(直接的和可传递的),可以运行以下Maven命令:

    mvn dependency:tree
    

    确保此列表中只有一个记录器依赖项。

    乍一看,我认为您必须将spring boot starter日志记录添加到spring boot starter测试中。

     类似资料:
    • 问题内容: 我必须在Hibernate,Jpa的应用程序(弹簧)中使用两个不同的数据库。我想直接将不同的表定义为不同的数据源。所以我使用两个不同的持久性单元,然后尝试使用 和 将不同的表放入不同的包中。但这不起作用。用第一个数据源来影响所有表。然后我尝试将诸如类的名称写入持久性XML文件 和it.two.app.domain.second.OtherTable 但是,当我运行Log时,说表’fir

    • 父项目为demo-mybatis子模块为mybatis-dao、demo-service。demo-mybatis pom.xml如下: 4.0.0 pom mybatis-DAO Demo-service Demo-mybatis-app org.springframework.Boot spring-boot-starter-parent 2.3.2.release com.example D

    • 我的父pom.xml如下所示: Web中的Application.java: web pom.xml: 启动时未发现服务impl模块中的bean。 谢谢

    • 我有一个带有多个模块的gradle(Android)库,有些模块依赖于其他模块(与这里的答案非常相似)https://stackoverflow.com/a/41761846/906362) 这对开发很有用,因为我可以对任何模块进行更改,并看到它们反映在其他模块上。但是要将构建发布到,我需要依赖项作为它们的在线版本。比如: 因此,当它被添加到项目中时,它正确地引用了在上找到的版本,而不是在使用时添

    • 好吧,我就直接说吧。我有一个使用Spring的多模块maven项目。项目结构可概括如下: 在本例中,根级pom文件将spring-boot-starter声明为父级: > 我可以分离出依赖项,以便模块1和模块2将spring-boot-starter-parent作为它们的父项,并从根pom中删除spring-boot-starter,从而释放common。 我可以将根pom作为父级删除到Comm

    • 我们正在寻找不同模块分别指定@ComponentScan、@EnableJpaRepositories和EntityManagerFactory的方法。setPackagesToScan。 我们将多个代码模块组合到Web应用程序中。此外,我们允许对基本代码进行客户特定的扩展,可以添加额外的包。在我的测试中,我发现我可以添加一个额外的java配置类,并且在@ComponentScan和@Enable