使用Java 17和Spring Boot 3.2.3 @Mapper无法创建Bean

问题描述 投票:0回答:1

我从过去 3 年开始就在 Java 11 和 Spring boot 2.6.6 中使用 Mapstruct。最近将项目依赖项更新为 Java 17 和 Spring Boot 3.2.3。有一次我在创建 Mapper 的 beans 时遇到了问题

Description:

   Field myMapper in com.abc.portal.service.UserService required a bean of type 
  'com.abc.portal.mapper.UserMapper' that could not be found.


Action:

Consider defining a bean of type 'com.abc.portal.mapper.UserMapper' in your configuration.

另一方面,我在 pom.xml 中有所有必需的插件

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                    <path>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>5.0.0</version>
                        <classifier>jakarta</classifier>
                    </path>
                    <path>
                        <groupId>jakarta.persistence</groupId>
                        <artifactId>jakarta.persistence-api</artifactId>
                        <version>3.1.0</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>
                        -Amapstruct.defaultComponentModel=spring
                    </compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
        

只是为了仔细检查我已经尝试过

@Mapper(componentModel = "spring") and mvn clean install

还有。但我仍然没有运气。任何建议将不胜感激。

更新 自动生成文件正在生成

spring-boot mapstruct java-17 mapper
1个回答
0
投票

在pom.xml中添加插件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.5.0</version>
    <executions>
      <execution>
        <id>add-source</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>target/generated-sources/annotations</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>

然后执行

mvn 全新安装

但请注意 - 从终端而不是从 IDE 运行 mvn 命令。 一旦 mvn clean install 成功,请勿清理您的项目。

© www.soinside.com 2019 - 2024. All rights reserved.