java.lang.ClassNotFoundException:无法找到实现

时间:2018-04-18 23:45:52

标签: java spring eclipse gradle mapstruct

我需要以下错误的帮助:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in file [D:\projects\PIK\bin\main\edu\pw\eiti\pik\user\UserController.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [edu.pw.eiti.pik.user.UserController]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for edu.pw.eiti.pik.user.UserMapper
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at edu.pw.eiti.pik.PikApplication.main(PikApplication.java:10) [main/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [edu.pw.eiti.pik.user.UserController]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for edu.pw.eiti.pik.user.UserMapper
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:182) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 18 common frames omitted
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for edu.pw.eiti.pik.user.UserMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:79) ~[mapstruct-jdk8-1.2.0.Final.jar:na]
    at edu.pw.eiti.pik.user.UserMapper.getInstance(UserMapper.java:12) ~[main/:na]
    at edu.pw.eiti.pik.user.UserController.<init>(UserController.java:18) ~[main/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:170) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find implementation for edu.pw.eiti.pik.user.UserMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:93) ~[mapstruct-jdk8-1.2.0.Final.jar:na]
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:76) ~[mapstruct-jdk8-1.2.0.Final.jar:na]
    ... 27 common frames omitted

UserMapper界面

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)

注释。 我使用eclipse Neon和buildship插件,org.mapstruct和spring都是用Gradle导入的。 它对我的同事的IntelliJ

没有这个错误

UserMapper的代码:

package edu.pw.eiti.pik.user;

import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import org.springframework.security.core.GrantedAuthority;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface UserMapper {

    static UserMapper getInstance() {
        return Mappers.getMapper(UserMapper.class);
    }

    User fromDto(UserDto dto);

    UserDto toDto(User entity);

    Authority fromDto(AuthorityDto dto);

    AuthorityDto toDto(GrantedAuthority entity);
}

从我读到的内容可能会导致编译器无法连接自动生成的代码(Gradle在AnnotationProcessing&gt; FactoryPath中没有设置任何内容)

3 个答案:

答案 0 :(得分:1)

使用Mapstruct with Spring时,您可以定义映射器应使用componentModel生成@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = "spring") public interface UserMapper { /* your code... */ } 生成Spring bean。

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    //rest of your code...
}

这允许您在bean中注入映射器:

UserMapper

考虑到Mapstruct在预编译阶段生成代码,因此在编译代码并查看生成的代码文件夹之前,您将看不到任何实现aes_接口的类。

答案 1 :(得分:1)

找到可能的原因:

https://github.com/mapstruct/mapstruct/issues/1159

看起来像mapstruct和lombok之间的问题

编辑:

解决方法是从eclipse.ini中删除javaagent:lombok.jar,重新启动IDE并重建项目,复制从注释生成的文件,将javaagent添加到eclipse.ini,重新启动IDE并重建项目并恢复复制的文件

答案 2 :(得分:0)

在您的编译器插件中添加注解mapstruct和lombok处理器,并且mapstruct应该为您的mapper界面生成实现:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</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>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${maven-compiler-plugin.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>