MapStruct not generating Component for mapper implementation

3,908 views
Skip to first unread message

Stork89

unread,
Mar 22, 2018, 5:35:29 PM3/22/18
to mapstruct-users
Hello,
I am in the middle of learning spring in an online course and one of the course parts uses MapStruct.
Unfortunately I fail to start spring boot because of failed dependency (Bean cannot be injected because no impl was found).

I looked into generated sources and it turns out that MapperImpl has a missing @Component annotation. The course instructor also showed generated source of the mapper which had @Component attached to the implementation.

The source code can be found here: git clone https://github.com/springframeworkguru/spring5-mvc-rest.git
The branch that is causing the problem: category-controller

My OS: win10
IDE: idea pro 2017.3
MapStruct ver: 1.2.0.Final

The generated code I get:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2018-03-22T22:24:43+0100",
comments = "version: 1.2.0.Final, compiler: javac, environment: Java 1.8.0_131 (Oracle Corporation)"
)
public class CategoryMapperImpl implements CategoryMapper {

@Override
public CategoryDTO categoryToCategoryDTO(Category category) {
if ( category == null ) {
return null;
}

CategoryDTO categoryDTO = new CategoryDTO();

categoryDTO.setId( category.getId() );
categoryDTO.setName( category.getName() );

return categoryDTO;
}
}

I checked the documentation and seems everything is correctly setup in pom (well, the instructor used the same code and it works on his side).
How can I force Mapper implementation to have @Component annotation?

Andreas Gudian

unread,
Mar 22, 2018, 5:43:32 PM3/22/18
to Stork89, mapstruct-users
The easiest way to make sure of that is to use the componentModel attribute in the @Mapper annotation (or @MapperConfig). 
That will make you more independent from quirks with IDEs that may or may not pull in the compiler processor options ;-).



--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stork89

unread,
Mar 23, 2018, 1:10:23 PM3/23/18
to mapstruct-users
Andreas, thank you for the hint. It actually solves my issue.

I also had a look into pom.xml and found a way of making it work.
I actually had following lines in pom for maven-compiler-plugin:
<compilerArgs>
<compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
</compilerArgs>

and changing this to 

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

makes @Component in the generated mapper. Not sure why but it works :)...

Filip Hrisafov

unread,
Mar 30, 2018, 7:24:50 AM3/30/18
to mapstruct-users
In case you are using IntelliJ be aware that your compiler arguments will not be picked up by IntelliJ when building your code. Have a look at IDEA-143742 for more information
Reply all
Reply to author
Forward
0 new messages