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

在启动spring应用程序期间无法注入mapstruct接口

徐俊人
2023-03-14

我有以下映射器(mapstruct版本1.3.1.Final)。

@Mapper(componentModel = "spring", uses = {}, unmappedTargetPolicy = ReportingPolicy.WARN)
public interface AccountMapper {
    @Mapping(source = "registrationDto.email", target = "email")
    @Mapping(source = "passwordDto.hashPassword", target = "password")
    Account from(RegistrationDto registrationDto, PasswordDto passwordDto);
}

当我尝试运行spring应用程序时,遇到了一个问题,即找不到与Mapper关联的bean。

Parameter 1 of constructor in com.xx.xx.Controller required a bean of type 'com.xxx.AccountMapper' that could not be found.

Consider defining a bean of type 'com.xxx.AccountMapper' in your configuration.

我试着用装修工解决这个问题。通过为接口添加注释@DecoratedWith(AccountMapperDecorator.class)并创建以下类。

@Component
public abstract class AccountMapperDecorator implements AccountMapper {

    @Autowired
    @Qualifier("delegate")
    private AccountMapper delegate;

    @Override
    public Account from(RegistrationDto registrationDto, PasswordDto passwordDto) {
        return delegate.from(registrationDto, passwordDto);
    }
}

然后我收到。

No qualifying bean of type 'com.xxx.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="delegate")}

在这种情况下可能有什么问题?

共有2个答案

萧树
2023-03-14

这种情况时有发生,
如果您注意到,则可能AccountMapperImpl没有生成到target/build文件夹。有几种方法可以解决此问题:

  • 尝试清理/构建目录
  • 运行 mvn 干净编译gradle 干净编译Java
  • 将 IDE 设置为使用执行/分级运行代码
  • 确保已启用“注释处理”
方通
2023-03-14

你运行了mvn包吗?当你在你的IDE中搜索classAccount tMapperImpl时,你能找到它吗?如果没有,那就是一个问题。如果你找不到它,Spring也不会。

也许您忘记在您的pom.xml?中配置(或配置错误)mapstruct处理器。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>...</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

那里?

 类似资料:
  • 我有一个Spring Boot应用程序,它有一些外部依赖(例如,项目之外的文件,需要存在,以便应用程序正确启动)。 和一系列其他例外。有没有一种方法可以恰当地做到这一点?

  • spring-boot-starter-parent使用1.5.9 依赖关系: 应用: 错误: [12 Sep 2018 20:36:26,702][错误]org.springframework.boot.springapplication:应用程序启动失败java.lang.nosuchmethoder错误:org.springframework.boot.builder.springappli

  • 我下面介绍了spring的RESTAPI,但我无法让它在tomcat服务器上运行(尝试了tomcat 7和9)。在eclipse中,我确实以- 编辑 尝试在项目上安装mvn,然后java-jarproject.jar 编辑2 我将项目导出到一个可运行的jar(并将所需的库打包到生成的jar中),但是 提供 编辑3 我没有删除我的m2,因为那里有很多东西,但我现在通过eclipse的spring工具

  • 我已经在tomcat服务器上安装了应用程序。在启动和加载应用程序的过程中,我有以下堆栈跟踪的错误。如何解决这个问题? apache-tomcat-7.0.47/webapps/petclinic 2016年10月27日下午12:14:36 org。阿帕奇。卡塔琳娜。果心标准上下文起始内部 严重:ServletContainerInitializer处理javax时出错。servlet。Servle

  • 我试图使用Intellij启动一个Spring应用程序,但我得到了下面的错误。我尝试将Tomcat依赖项放在pom.xml中,但这并不奏效(我后来也发现不需要这样做)。 应用程序应该可以正常工作,因为它已经在一个同事的电脑上工作了,所以我不太确定为什么我会得到这个错误。我已经尝试了几个修复,我在这里遇到了,但没有一个奏效。下面是我的: