mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#664 Annotate mapper implementations with @Singleton when using jsr330.
This commit is contained in:
parent
f0559fca43
commit
5ede0e91db
@ -123,9 +123,9 @@ import java.lang.annotation.Target;
|
|||||||
* <p>
|
* <p>
|
||||||
* JSR 330 doesn't specify qualifiers and only allows to specifically name the beans. Hence, the generated
|
* JSR 330 doesn't specify qualifiers and only allows to specifically name the beans. Hence, the generated
|
||||||
* implementation of the original mapper is annotated with {@code @Named("fully-qualified-name-of-generated-impl")}
|
* implementation of the original mapper is annotated with {@code @Named("fully-qualified-name-of-generated-impl")}
|
||||||
* (please note that when using a decorator, the class name of the mapper implementation ends with an underscore). To
|
* and {@code @Singleton} (please note that when using a decorator, the class name of the mapper implementation ends
|
||||||
* inject that bean in your decorator, add the same annotation to the delegate field (e.g. by copy/pasting it from the
|
* with an underscore). To inject that bean in your decorator, add the same annotation to the delegate field (e.g. by
|
||||||
* generated class):
|
* copy/pasting it from the generated class):
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* public abstract class PersonMapperDecorator implements PersonMapper {
|
* public abstract class PersonMapperDecorator implements PersonMapper {
|
||||||
|
@ -75,7 +75,7 @@ public @interface Mapper {
|
|||||||
* can be retrieved via {@code @Autowired}</li>
|
* can be retrieved via {@code @Autowired}</li>
|
||||||
* <li>
|
* <li>
|
||||||
* {@code jsr330}: the generated mapper is annotated with {@code @Named} and
|
* {@code jsr330}: the generated mapper is annotated with {@code @Named} and
|
||||||
* can be retrieved via {@code @Inject}</li>
|
* {@code @Singleton}, and can be retrieved via {@code @Inject}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* The method overrides an unmappedTargetPolicy set in a central configuration set
|
* The method overrides an unmappedTargetPolicy set in a central configuration set
|
||||||
* by {@link #config() }
|
* by {@link #config() }
|
||||||
|
@ -77,7 +77,7 @@ public @interface MapperConfig {
|
|||||||
* can be retrieved via {@code @Autowired}</li>
|
* can be retrieved via {@code @Autowired}</li>
|
||||||
* <li>
|
* <li>
|
||||||
* {@code jsr330}: the generated mapper is annotated with {@code @Named} and
|
* {@code jsr330}: the generated mapper is annotated with {@code @Named} and
|
||||||
* can be retrieved via {@code @Inject}</li>
|
* {@code @Singleton}, and can be retrieved via {@code @Inject}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @return The component model for the generated mapper.
|
* @return The component model for the generated mapper.
|
||||||
|
@ -23,7 +23,9 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
@Named
|
@Named
|
||||||
public class DateMapper {
|
public class DateMapper {
|
||||||
|
|
||||||
|
@ -42,16 +42,16 @@ public class Jsr330ComponentProcessor extends AnnotationBasedComponentModelProce
|
|||||||
@Override
|
@Override
|
||||||
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
||||||
if ( mapper.getDecorator() == null ) {
|
if ( mapper.getDecorator() == null ) {
|
||||||
return Collections.singletonList( named() );
|
return Arrays.asList( singleton(), named() );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return Collections.singletonList( namedDelegate( mapper ) );
|
return Arrays.asList( singleton(), namedDelegate( mapper ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<Annotation> getDecoratorAnnotations() {
|
protected List<Annotation> getDecoratorAnnotations() {
|
||||||
return Collections.singletonList( named() );
|
return Arrays.asList( singleton(), named() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -69,6 +69,10 @@ public class Jsr330ComponentProcessor extends AnnotationBasedComponentModelProce
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Annotation singleton() {
|
||||||
|
return new Annotation( getTypeFactory().getType( "javax.inject.Singleton" ) );
|
||||||
|
}
|
||||||
|
|
||||||
private Annotation named() {
|
private Annotation named() {
|
||||||
return new Annotation( getTypeFactory().getType( "javax.inject.Named" ) );
|
return new Annotation( getTypeFactory().getType( "javax.inject.Named" ) );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user