MapStruct - @Mapper注释不创建bean

时间:2017-11-04 10:40:35

标签: spring mapper mapstruct

我从此来源https://github.com/springframeworkguru/spring5-mvc-rest/tree/vendor-api下载了应用程序 我对MapStruct有问题。

@Mapper
public interface CategoryMapper {

CategoryMapper INSTANCE = Mappers.getMapper(CategoryMapper.class);


CategoryDTO categoryToCategoryDTO(Category category);

}

@Data
public class CategoryDTO {
    private Long id;
    private String name;
}

域类:

@Data
@Entity
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
}

服务类:

@Service
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;
    private final CategoryRepository categoryRepository;

    public CategoryServiceImpl(CategoryMapper categoryMapper, CategoryRepository categoryRepository) {
        this.categoryMapper = categoryMapper;
        this.categoryRepository = categoryRepository;
    }
}

在pom.xml依赖项中,我只粘贴了两个::

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <org.mapstruct.version>1.2.0.CR2</org.mapstruct.version>
</properties>
<dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-jdk8</artifactId>
        <version>${org.mapstruct.version}</version>
</dependency>

插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</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>
        </annotationProcessorPaths>
        <compilerArgs>
           <compilerArg>
                 -Amapstruct.defaultComponentModel=spring
           </compilerArg>
        </compilerArgs>
    </configuration>
</plugin>

说明

Parameter 0 of constructor in 
guru.springfamework.services.CategoryServiceImpl required a bean of type 'guru.springfamework.api.v1.mapper.CategoryMapper' that could not be found.

动作:

Consider defining a bean of type 'guru.springfamework.api.v1.mapper.CategoryMapper' in your configuration.

我认为在我的Intellij注释中@Mapper不会为mapper创建一个bean。 我没有改变John GitHub的代码。 任何的想法? 我试图将路径生成的源更改为目标,但这并没有帮助 谢谢你的帮助。

9 个答案:

答案 0 :(得分:4)

我通过

解决了错误
  1. mvn clean install
  2. 还要将其添加到您的pom

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</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>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>
                           -Amapstruct.defaultComponentModel=spring
                    </compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
    

答案 1 :(得分:3)

为了将映射器用作bean并使用@Autowired,只需将映射器声明为@Mapper(componentModel = "spring")。然后将其注入您需要的任何地方。

答案 2 :(得分:1)

问题是使用@Mapper构造将生成一个没有的类 默认情况下@Component注释。

您可能没有使用Maven来编译项目。确保运行Maven cleaninstall,或手动传递编译器参数mapstruct.defaultComponentModel=spring

因为您正在使用:

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

这会告诉MapStruct生成Spring Component

如果您检查已编译的目标类,并在反编译器中打开它(如果您的IDE支持),则应该有一个{@ 1}}注释@Component。

答案 3 :(得分:0)

我认为这不仅仅适用于IntelliJ。存在一个已知问题,其中IntelliJ实际上没有从maven编译器annotationProcessorPaths中获取注释处理器(有关详细信息,请参阅IDEA-150621)。

最重要的是,链接存储库中的示例是针对MapStruct的最佳实践。使用spring componentModel时,永远不应使用Mappers.getMapper。原因是工厂无法正确构造映射器,因为它应该通过弹簧构造。此外,编译器参数mapstruct.defaultComponentModel可能会破坏与IDE的集成,因为它没有被拾取(您还需要在IntelliJ设置中设置它)

答案 4 :(得分:0)

我有同样的问题。在Gradle中,我通过添加新的依赖项解决了该问题:

compileOnly('org.mapstruct:mapstruct-processor:1.3.0.Beta2')

所有必要的依赖关系应如下:

compile('org.mapstruct:mapstruct:1.3.0.Beta2')
compileOnly('org.mapstruct:mapstruct-processor:1.3.0.Beta2')
annotationProcessor('org.mapstruct:mapstruct-processor:1.3.0.Beta2')

答案 5 :(得分:0)

您必须从IntelliJ中启用注释处理器,

设置->构建,执行,部署->编译器->注释 处理器,然后单击启用批注处理复选框。

答案 6 :(得分:0)

您首先必须像这样使用Mapper

To use mappers as beans and use **@Autowired**,
just declare mappers as @Mapper(componentModel = "spring"). 
Then just inject it wherever you need this.

此外,每次创建新的映射器类时,切记要进行 mvn全新安装

它将在目标文件夹中创建映射器接口的实现文件,然后您可以运行Spring项目。

答案 7 :(得分:0)

在您的Mapper类中,使用@Mapper(componentModel = "spring")而不是@Mapper

run mvn clean install就是这样!完成了!

及其替代方法是在 pom.xml 插件中定义编译器参数,如下所示!

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

答案 8 :(得分:0)

这对我有用:

  1. POM 依赖项:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <scope>provided</scope>
</dependency>

  1. POM 插件:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
  1. componentModel = "spring" 注释中有 @MapStruct 参数。
  2. 在您的项目上运行 mvn clean

我不认为你真的需要在你的 POM 中声明以下编译器参数,因为 ``componentModel = "spring"` 就足够了。

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

我从 JHipster 生成的微服务应用程序项目中获取了这些代码片段。