英文:
@Mapper UnsatisfiedDependencyException while deploying
问题
我在服务类中的@Autowired private ApplicantDetailMapper mapper;
这行代码处遇到了UnsatisfiedDependecyException
异常。在服务类中,存储库也被自动装配了,并且它们是正常工作的,因此可以假设组件扫描是有效的。
我对这个问题感到困惑。
使用Spring的Mapper接口
@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)}
英文:
I'm getting UnsatisfiedDependecyException at @Autowired private ApplicantDetailMapper mapper;
in service class. In service class repositories are autowried too and they are working, so assume component scan is working.
I'm clueless about this issue.
Mapper interface using spring
@Mapper(componentModel = "spring")
public interface ApplicantDetailMapper {
ApplicantEntity dtoToEntity(ApplicantDto source);
}
Service in which it is autowired
@Service
@Transactional
public class ApplicantDetailServiceImpl implements ApplicantDetailService {
@Autowired
private ExampleRepository repo;
@Autowired
private ApplicantDetailMapper mapper;
}
set up in 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>
Exception
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)}
EDIT
Eclipse IDE For Enterprise Java Developers 2020-06
答案1
得分: 1
根据官方文档:
> 如果您正在使用Eclipse IDE,请确保安装了当前版本的M2E插件。当导入配置如上所示的Maven项目时,它会设置MapStruct注解处理器,以便在IDE中保存映射器类型时运行。很不错,是吗?
要确保一切按预期工作,转到项目属性,然后选择"Java Compiler" → "Annotation Processing" → "Factory Path"。
MapStruct处理器的JAR文件应在列表中列出并启用。通过编译器插件配置的任何处理器选项(参见下文)应在"Java Compiler" → "Annotation Processing" 下列出。
如果处理器没有生效,请检查通过M2E启用注解处理器的配置。要这样做,请转到"Preferences" → "Maven" → "Annotation Processing",然后选择"Automatically configure JDT APT"。
或者,在您POM文件的属性部分中指定以下内容:
<m2e.apt.activation>jdt_apt</m2e.apt.activation>。
还要确保您的项目使用Java 1.8或更高版本(项目属性 → "Java Compiler" → "Compile Compliance Level")。它不适用于旧版本。
因此,正如它所说的,"Factory Path" 应该显示列出并启用 MapStruct Jar。在我的情况下,根本没有显示出来。
设置 "Automatically configure JDT APT"
导致Eclipse显示Maven更新错误,错误详情如下:
> 更新Maven时遇到问题,无法同时选择Utility模块和Dynamic Web模块3.1。
因此,对我来说,在 pom.xml
的属性中添加 <m2e.apt.activation>jdt_apt</m2e.apt.activation>
解决了这个问题。
来源: MapStruct文档
英文:
According to the official documentation
> 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.
So as it says Factory Path should show MapStruct Jar listed and enabled there. In my case it wasn't there at all.
Setting "Automatically configure JDT APT"
caused eclipse to show Maven Update Error. with details as
> update maven encountered problem Utility Module and Dynamic Web Module 3.1 cannot both be selected.
So For me adding <m2e.apt.activation>jdt_apt</m2e.apt.activation>
in pom.xml properties
solved the problem.
Source: MapStruct Documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论