部署时出现@Mapper UnsatisfiedDependencyException
@Mapper UnsatisfiedDependencyException while deploying
我在服务 class 的 @Autowired private ApplicantDetailMapper mapper;
收到 UnsatisfiedDependecyException。在服务中 class 存储库也是自动编写的并且它们正在工作,因此假设组件扫描正在工作。
我对这个问题一无所知。
Mapper 接口使用 spring
@Mapper(componentModel = "spring")
public interface ApplicantDetailMapper {
ApplicantEntity dtoToEntity(ApplicantDto source);
}
自动装配的服务
@Service
@Transactional
public class ApplicantDetailServiceImpl implements ApplicantDetailService {
@Autowired
private ExampleRepository repo;
@Autowired
private ApplicantDetailMapper mapper;
}
设置于pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.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>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
<properties>
<org.mapstruct.version>1.4.0.Final</org.mapstruct.version>
</properties>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
异常
Unsatisfied dependency expressed through field 'applicantDetailService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicantDetailServiceImpl': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dtos.mappers.ApplicantDetailMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
编辑
Eclipse IDE 企业版 Java 开发人员 2020-06
根据官方文档
If you are working with the Eclipse IDE, make sure to have a current version of the
M2E plug-in. When importing a Maven project configured as shown above, it will
set up the MapStruct annotation processor so it runs right in the IDE, whenever
you save a mapper type. Neat, isn’t it?
To double check that everything is working as expected, go to your project’s
properties and select "Java Compiler" → "Annotation Processing" → "Factory Path".
The MapStruct processor JAR should be listed and enabled there. Any processor
options configured via the compiler plug-in (see below) should be listed under
"Java Compiler" → "Annotation Processing".
If the processor is not kicking in, check that the configuration of annotation
processors through M2E is enabled. To do so, go to "Preferences" → "Maven" →
"Annotation Processing" and select "Automatically configure JDT APT".
Alternatively, specify the following in the properties section of your POM file:
<m2e.apt.activation>jdt_apt</m2e.apt.activation>.
Also make sure that your project is using Java 1.8 or later (project properties →
"Java Compiler" → "Compile Compliance Level"). It will not work with older
versions.
正如它所说,工厂路径应该显示列出并启用的 MapStruct Jar。就我而言,它根本不存在。
设置 "Automatically configure JDT APT"
导致 eclipse 显示 Maven 更新错误。详细信息为
update maven encountered problem Utility Module and Dynamic Web Module 3.1 cannot both be selected.
所以对我来说,在 pom.xml properties
中添加 <m2e.apt.activation>jdt_apt</m2e.apt.activation>
解决了问题。
我在服务 class 的 @Autowired private ApplicantDetailMapper mapper;
收到 UnsatisfiedDependecyException。在服务中 class 存储库也是自动编写的并且它们正在工作,因此假设组件扫描正在工作。
我对这个问题一无所知。
Mapper 接口使用 spring
@Mapper(componentModel = "spring")
public interface ApplicantDetailMapper {
ApplicantEntity dtoToEntity(ApplicantDto source);
}
自动装配的服务
@Service
@Transactional
public class ApplicantDetailServiceImpl implements ApplicantDetailService {
@Autowired
private ExampleRepository repo;
@Autowired
private ApplicantDetailMapper mapper;
}
设置于pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.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>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
<properties>
<org.mapstruct.version>1.4.0.Final</org.mapstruct.version>
</properties>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
异常
Unsatisfied dependency expressed through field 'applicantDetailService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicantDetailServiceImpl': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dtos.mappers.ApplicantDetailMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
编辑
Eclipse IDE 企业版 Java 开发人员 2020-06
根据官方文档
If you are working with the Eclipse IDE, make sure to have a current version of the M2E plug-in. When importing a Maven project configured as shown above, it will set up the MapStruct annotation processor so it runs right in the IDE, whenever you save a mapper type. Neat, isn’t it? To double check that everything is working as expected, go to your project’s properties and select "Java Compiler" → "Annotation Processing" → "Factory Path". The MapStruct processor JAR should be listed and enabled there. Any processor options configured via the compiler plug-in (see below) should be listed under "Java Compiler" → "Annotation Processing". If the processor is not kicking in, check that the configuration of annotation processors through M2E is enabled. To do so, go to "Preferences" → "Maven" → "Annotation Processing" and select "Automatically configure JDT APT". Alternatively, specify the following in the properties section of your POM file: <m2e.apt.activation>jdt_apt</m2e.apt.activation>. Also make sure that your project is using Java 1.8 or later (project properties → "Java Compiler" → "Compile Compliance Level"). It will not work with older versions.
正如它所说,工厂路径应该显示列出并启用的 MapStruct Jar。就我而言,它根本不存在。
设置 "Automatically configure JDT APT"
导致 eclipse 显示 Maven 更新错误。详细信息为
update maven encountered problem Utility Module and Dynamic Web Module 3.1 cannot both be selected.
所以对我来说,在 pom.xml properties
中添加 <m2e.apt.activation>jdt_apt</m2e.apt.activation>
解决了问题。