Java / MapStruct Java Interview questions part 2
What is the componentModel attribute in @Mapper?
componentModel tells MapStruct how the generated mapper implementation should be made available for use. It changes only the instantiation strategy, not the mapping logic itself.
| Value | Behavior |
default | Plain class, accessed via Mappers.getMapper(...). |
spring | Generated class annotated with @Component for autowiring. |
cdi | Generated as a CDI-managed bean. |
jsr330 | Annotated with @Named for JSR-330 injection. |
Picking the right value mostly comes down to how the application already manages its beans - a Spring Boot project almost always uses spring so mappers can be autowired like any other service, while a plain library or a small standalone tool is usually fine leaving it at default and fetching the mapper through Mappers.getMapper(...) when needed.
More Related questions...