简体   繁体   English

无法自动装配 MapStruct 映射器

[英]Unable to autowire the MapStruct mapper

I was following this tutorial about the partial update.我正在关注有关部分更新的本教程 As instructed, I created mapper interface with proper annotations.按照指示,我创建了带有适当注释的映射器界面。

Here's the mapper这是映射器

@Mapper(componentModel = "spring")
public interface UserEntityMapper {
  
  @Mapping(source = "password", target = "password")
  @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
  void updatePasswordFromDTO(PasswordResetRequest dto, @MappingTarget User entity);
}

As per the tutorial, @Mapper(componentModel = "spring") generates mapper as a Spring bean that can be retrieved via @Autowired .根据教程, @Mapper(componentModel = "spring")生成映射器作为可以通过@Autowired检索的 Spring bean。

But when I tried to do same in my service layer class ,但是当我尝试在我的服务层class做同样的事情时,

@Service
@Transactional
public class AccountServiceImpl implements IAccountService {
  ...
  @Autowired
  private UserEntityMapper userMapper;
  ...
}

I get this error, my application failed to start.我收到此错误,我的应用程序无法启动。

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userMapper in com.application.services.AccountServiceImpl required a bean of type 'com.application.mappers.UserEntityMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.application.mappers.UserEntityMapper' in your configuration.

And at last, here's pom.xml of my 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 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.5.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>authentication-template</name>
    <description>Description</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.4.2.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I also tried these different solution, but same result.我也尝试过这些不同的解决方案,但结果相同。

  1. Answer By @Som由@Som 回答
  2. Answer by @Gunnar @Gunnar 的回答

Edit 1编辑 1

Here's @SpringBootApplication file.这是@SpringBootApplication文件。

@SpringBootApplication
@PropertySources(value = {
  @PropertySource("classpath:mail.properties"),
  @PropertySource("classpath:messages.properties"),
  @PropertySource("classpath:security.properties")
})
public class Application {

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

}

Edit 2编辑 2

Package structure of the project项目的包结构

项目的包结构

Please help, Thank you :)请帮忙,谢谢:)

There are several suggestions you can investigate.您可以调查几个建议。 First, check if there is @Component in the class generated by mvn.首先检查mvn生成的类中是否有@Component。

  • If so, check if your application is configured to scan that package.如果是,请检查您的应用程序是否配置为扫描该包。
  • If no, try to read the MapStruct documentation .如果不是,请尝试阅读MapStruct 文档

I have the dependency like below, all the things are Ok.我有如下依赖,一切正常。

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.2.Final</version>
</dependency>

And in part of plugins.以及部分插件。


 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                      <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.4.2.Final</version>
                      </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

If the pom.xml you provided is the latest version, I think you are missing the step, that actually generates the MapStruct mappers from your definitions.如果您提供的 pom.xml 是最新版本,我认为您错过了根据您的定义实际生成 MapStruct 映射器的步骤。 You need to define it as part of the build setup as stated here https://mapstruct.org/documentation/installation/您需要将其定义为构建设置的一部分,如此处所述https://mapstruct.org/documentation/installation/

You need to add that part to the build - plugins section您需要将该部分添加到 build - plugins 部分

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source> <!-- depending on your project -->
                <target>1.8</target> <!-- depending on your project -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <!-- other annotation processors -->
                </annotationProcessorPaths>
            </configuration>
        </plugin>

Also check your target/generated folders if the mappers are successfully generated如果映射器成功生成,还要检查您的目标/生成的文件夹

Added to plugins:添加到插件:

 <compilerArgs>
      <compilerArg>
             -Amapstruct.defaultComponentModel=spring
      </compilerArg>
 </compilerArgs>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM