@Mapper部分:在部署过程中出现了UnsatisfiedDependencyException异常。

huangapple go评论73阅读模式
英文:

@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 = &quot;spring&quot;)
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

&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.8.1&lt;/version&gt;
				&lt;configuration&gt;
					&lt;source&gt;1.8&lt;/source&gt;
					&lt;target&gt;1.8&lt;/target&gt;
					&lt;annotationProcessorPaths&gt;
						&lt;path&gt;
							&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
							&lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt;
							&lt;version&gt;${org.mapstruct.version}&lt;/version&gt;
						&lt;/path&gt;
					&lt;/annotationProcessorPaths&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
		
	&lt;properties&gt;
      &lt;org.mapstruct.version&gt;1.4.0.Final&lt;/org.mapstruct.version&gt;
    &lt;/properties&gt;

    &lt;dependency&gt;
		&lt;groupId&gt;org.mapstruct&lt;/groupId&gt;
		&lt;artifactId&gt;mapstruct&lt;/artifactId&gt;
		&lt;version&gt;${org.mapstruct.version}&lt;/version&gt;
	&lt;/dependency&gt;

Exception

Unsatisfied dependency expressed through field &#39;applicantDetailService&#39;; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;applicantDetailServiceImpl&#39;: Unsatisfied dependency expressed through field &#39;mapper&#39;; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type &#39;com.dtos.mappers.ApplicantDetailMapper&#39; 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。在我的情况下,根本没有显示出来。

设置 &quot;Automatically configure JDT APT&quot; 导致Eclipse显示Maven更新错误,错误详情如下:

> 更新Maven时遇到问题,无法同时选择Utility模块和Dynamic Web模块3.1。

因此,对我来说,在 pom.xml 的属性中添加 &lt;m2e.apt.activation&gt;jdt_apt&lt;/m2e.apt.activation&gt; 解决了这个问题。

来源: 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 &quot;Automatically configure JDT APT&quot; 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 &lt;m2e.apt.activation&gt;jdt_apt&lt;/m2e.apt.activation&gt; in pom.xml properties solved the problem.

Source: MapStruct Documentation

huangapple
  • 本文由 发表于 2020年10月4日 18:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64193455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定