GeezerVison is showing github.com — @Mapper(componentModel = "spring") doesn't support custom name for Spring Service Annotation · Issue #1427 · mapstruct/mapstruct
@Mapper(componentModel = "spring") doesn't support custom name for Spring Service Annotation #1427DescriptionCan't specify a name for the Spring Service annotation
If the interface definition something like
@Mapper(componentModel = "spring", componentModelName = "cakeMapperV2")
public interface CakeMapper{
Cake map(Cupcake cupcake);
}
@Service("cakeMapperV2")
public class CakeMapperImpl {
}
Reacted by Matteo, Simon Tenbeitel, Stephen Fox, Semyon Levin, Jim and Nikhil Nagrale Reacted by Dmitriy Belyaev Activityfiliphr commented on Apr 14, 2018 @zurbergram yes this is not possible in the moment out of the box.
However, there is a way for you to achieve what you are looking for with some internal customizations. We have an internal SPI ModelElementProcessor that adds the spring @Component annotation. The one for Spring is the SpringComponentProcessor.
What you can do is to define your own ModelElementProcessor that would be of a custom component model, provide your own marker annotation for the name of the component and you would be able to do what you are looking for.
Just keep in mind that this is an internal SPI, which means that we cannot guarantee backwards compatibility between versions. However, it is rarely changed. chris922 commented on Apr 18, 2018 Oups, sorry! I wasn't aware about the fact that changes in my forked mapstruct repo will directly be linked to this issue. Hopefully I haven't spammed you with too much notification mails.
@filiphr as my changes are now linked here maybe you could already take a look or should I create a PR? (this is my more or less first 'real' OS contribution; right now I am waiting that the built finishs locally including all tests)
I tried to add the possibility to have an extra annotation (@MapperSpringConfig / @MapperJsr330Config) to be able to have more component model specific configurations, e. g. if @Service or @Component should be used and - as requested in this ticket - to define an own bean name.
Maybe its better to extend the @Mapper configuration with a new property like componentName, but my idea was that there are maybe also other configuration things that only belongs to a special componentModel and having a special annotation for this could be helpful for those usecases. jccampanero commented on Sep 4, 2022
Adding this to 1.6 in order to be able to think of a solution that would work correctly with #1574
@filiphr Please, could you elaborate the idea? filiphr commented on Sep 5, 2022 With #1574 one can now write a mapper that looks like:
@Mapper(componentModel = "spring")
@AnnotateWith(value = Component.class, elements = @Element(strings = "myCustomMapperName"))
public interface MyCustomMapper {
}
This will now generate
@Component("myCustomMapperName")
@Component
public interface MyCustomMapper {
}
This is of course invalid.
What we need to do is to make sure that we are properly handling this, i.e. not adding the @Component in AnnotationBasedComponentModelProcessor.
We'll need to make sure that the following things work:
- Works properly with delegator
- Works properly when a different specialization of
@Component is used. e.g. if @Service is used with @AnnotateWith
Reacted by Dmitriy Belyaev jccampanero commented on Sep 5, 2022 Thank you very much for your explanation @filiphr.
So, basically, it is necessary to finish the work already started on this commit, making sure it works properly with delegator and with other Spring related annotations, is that correct?
I will be glad to work in the issue if you wish. jccampanero commented on Sep 11, 2022 Hi @filiphr.
It's nice talking to you again.
I have been working on the issue, I have provided a possible implementation mainly based on the code that you and @Zegveld developed.
I tried performing some initial testing of the decorator related stuff and it seems to work properly.
One question came to mind though: when using a decorator, if we have a component where we have a custom name, shouldn't be that name the one used in the generated @Qualifier annotation instead of the default delegate? In the present code, it works properly because of the precedence of the @Qualifier annotation but it is a common practice in Spring to qualify by the custom name provided when annotating the component or service.
I created a PR with the changes performed: please, feel free to ask me to change anything you think is appropriate.
Thank you very much. added a commit that references this issue on Oct 1, 2022#1427 Add support for custom name in Spring stereotype annotations filiphr commented on Oct 1, 2022 This was implemented using the new @AnnotateWith.
This means that the one can do:
@AnnotateWith(value = Service.class, elements = @Element(strings = "cakeMapperV2"))
@Mapper(componentModel = "spring")
public interface CakeMapper{
Cake map(Cupcake cupcake);
}
And that will generate:
@Service("cakeMapperV2")
public class CakeMapperImpl {
}Reacted by salpreh, AlexElin, James Wang and Giulio Longfils Reacted by Carlos Freund and Zsolt Katona
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|