生成的 Mapstruct class 未被 Spring 注入到 JHipster 网络应用程序中

Mapstruct generated class not being injected by Spring in JHipster web app

我正在开发一个使用 JHipster v2.20.0 创建的网络应用程序。 使用 Eclipse 4.5.0 WTP 作为 IDE.

Spring 配置是基于注释的。

我刚刚将 this pull 与我们的代码合并。

当我尝试 运行 Eclipse 中的应用程序时,我得到了这个异常:

[INFO] com.app.tenancy.hibernate.MyCurrentTenantIdentifierResolver - MyCurrentTenantIdentifierResolver.getTenantId(): Couldn't find Company/Tenant for the domain inventario, stopping serving the request
[WARN] org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userManagementResource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.app.web.rest.mapper.UserManagementMapper com.app.web.rest.UserManagementResource.userManagementMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.app.web.rest.mapper.UserManagementMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at com.app.Application.main(Application.java:79) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.app.web.rest.mapper.UserManagementMapper com.app.web.rest.UserManagementResource.userManagementMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.app.web.rest.mapper.UserManagementMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 14 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.app.web.rest.mapper.UserManagementMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 16 common frames omitted

这是 UserManagementMapper class:

package com.app.web.rest.mapper;

import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import com.app.domain.User;
import com.app.service.UserService;
import com.app.web.rest.dto.UserManagementDTO;

@Mapper(componentModel = "spring", uses = {})
public abstract class UserManagementMapper {

    @Inject
    private UserService userService;

    public abstract UserManagementDTO userToUserManagementDTO(User user);
    public abstract List<UserManagementDTO> usersToUserManagementsDTO(List<User> users);

    @Mapping(target = "createdBy", ignore=true)
    @Mapping(target = "createdDate", ignore=true)
    @Mapping(target = "lastModifiedBy", ignore=true)
    @Mapping(target = "lastModifiedDate", ignore=true)
    @Mapping(target = "persistentTokens", ignore=true)
    @Mapping(target = "resetDate", ignore=true)
    @Mapping(target = "activationKey", ignore=true)
    @Mapping(target = "resetKey", ignore=true)
    @Mapping(target = "password", ignore=true)
    public abstract User updateUserFromDto(UserManagementDTO userManagementDTO, @MappingTarget User user);



    public User userManagementDTOToUser(UserManagementDTO userManagementDTO) {
        return  Optional.ofNullable(userService.getUserWithAuthorities(userManagementDTO.getId()))
            .map(user -> this.updateUserFromDto(userManagementDTO, user))
            .orElse(null);
    }
}

这是 UserManagementResource class:

package com.app.web.rest;

import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.annotation.Timed;
import com.app.domain.User;
import com.app.repository.UserRepository;
import com.app.security.AuthoritiesConstants;
import com.app.service.UserService;
import com.app.web.rest.dto.UserManagementDTO;
import com.app.web.rest.mapper.UserManagementMapper;
import com.app.web.rest.util.PaginationUtil;

/**
 * REST controller for managing users.
 */
@RestController
@RequestMapping("/api")
public class UserManagementResource {

    private final Logger log = LoggerFactory.getLogger(UserManagementResource.class);

    @Inject
    private UserService userService;

    @Inject
    private UserRepository userRepository;

    @Autowired
    private UserManagementMapper userManagementMapper;

    /**
     * GET  /userManagement -> get all users to manage.
     */
    @RequestMapping(value = "/userManagement",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @RolesAllowed(AuthoritiesConstants.ADMIN)
    @Transactional(readOnly = true)
    public ResponseEntity<List<UserManagementDTO>> getAll(@RequestParam(value = "page" , required = false) Integer offset,
                                                         @RequestParam(value = "per_page", required = false) Integer limit)
        throws URISyntaxException {
        Page<User> page = userRepository.findAll(PaginationUtil.generatePageRequest(offset, limit));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/userManagement", offset, limit);
        return new ResponseEntity<>(page.getContent().stream()
                 .map(userManagementMapper::userToUserManagementDTO)
                 .collect(Collectors.toCollection(LinkedList::new)), headers, HttpStatus.OK);
    }

    /**
     * GET  /userManagement/:id -> get id user to manage.
     */
    @RequestMapping(value = "/userManagement/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @RolesAllowed(AuthoritiesConstants.ADMIN)
    @Transactional(readOnly = true)
    ResponseEntity<UserManagementDTO> getUser(@PathVariable Long id) {
       log.debug("REST request to get User to manage : {}", id);
       return  Optional.ofNullable(userService.getUserWithAuthorities(id))
               .map(userManagementMapper::userToUserManagementDTO)
               .map(userManagementDTO -> new ResponseEntity<>(
                    userManagementDTO,
                    HttpStatus.OK))
               .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * PUT  /userManagement -> Updates an existing user.
     */
    @RequestMapping(value = "/userManagement",
        method = RequestMethod.PUT,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    @RolesAllowed(AuthoritiesConstants.ADMIN)
    public ResponseEntity<Void> update(@RequestBody UserManagementDTO userManagementDTO) throws URISyntaxException {
        log.debug("REST request to update User : {}", userManagementDTO);
        if (userManagementDTO.getId() == null) {
            return ResponseEntity.badRequest().header("Failure", "You cannot create a new user").build();
        }
        User user = userManagementMapper.userManagementDTOToUser(userManagementDTO);
        userRepository.save(user);
        return ResponseEntity.ok().build();
    }
}

最后,pom.xml 文件在此合并期间未更改。应用程序在合并之前工作正常。

这可能是一些新手问题,但我尝试解决这个问题比执行合并浪费了更多的时间。

在此先感谢您的帮助!

您应该确保 "target/generated-sources" 在 Eclipse 中链接为源文件夹。

我在使用 Gradle 的 JHipster 项目中遇到了同样的问题。在 Eclipse 中,我将 "build\generated\source\apt\main" 目录链接为源文件夹,问题就解决了。

  1. 右键单击您的项目并转到 "Properties"
  2. 转到 "Java Build Path" 并查找 "Source" 选项卡
  3. 单击按钮 "Link Source..."
  4. 浏览 "build\generated\source\apt\main" 目录
  5. 您可以保留建议的名称
  6. 点击完成

现在您可以尝试 运行 您的 JHipster 项目,在 eclipse 中,作为一个 Java 应用程序,它可能会工作。

来自JHipster documentation page for setting up Eclipse

Add apt generated source folder to build path

When using buildship gradles default outputfolder is filtered and not visible in your workspace. Therefore you need to remove it from eclipse’s resource filter setting.

  • Right click on your project and select Properties
  • Select Resources
  • Remove the entry build
  • Select Java Build Path
  • Click Add Folder...
  • Check the path build/generated/source/apt/main

Make sure the new source folder contains the correctly generated mapper implementations when running JHipster via eclipse.

对于 IntelliJ (Gradle) 用户,

Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable annotation processing

确保assemblegradle之后。

对于maven用户,

Build tools -> Maven -> Importing -> Generated Sources folder

几天前我开始使用intelliJ。 启用注释处理(如 Shree Krishna 所建议的那样)对我有用。 它记录在 JHipster IDEA 配置页面 (https://www.jhipster.tech/configuring-ide-idea/) 上。但是此页面上的当前文档对于像我这样的新 IntelliJ 用户来说并不明确。

Jhispter 成员应该通过更明确的内容更新他们的文档,例如 Shree Krishna 的回答(这将节省很多人花费数小时来解决问题的时间)。

Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable annotation processing

虽然没有使用 JHipster,但我在使用常规 spring 启动时遇到了同样的问题。

问题是,在 maven 注释处理器路径插件上,我已经为 MapStruct 路径指定了注释配置,但 maven 仍然会给出 unstaisfied dependency 错误。正在生成 mapstruct 映射器的实现。我生成的注释文件夹也存在于类路径中。还是没用。

修复?它是由 lombok 项目引起的。我没有在我的代码中使用项目 lombok,但它仍然出现在我的依赖树上。我在lombok的pom.xml文件上也指定了注释配置路径,最后,该死的东西被打包了。

我遇到了同样的问题post-升级到 Intellij 2020.3

这里的解决方案对我有用

-Djps.track.ap.dependencies=false

应该添加到 文件 |设置 |构建、执行、部署 |编译器 |用户本地构建过程 VM 选项字段.

来源:https://youtrack.jetbrains.com/issue/IDEA-250718

对于 Eclipse,安装插件 m2e-apt,然后确保将“target/generated-sources”链接为源文件夹