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>
|
||||
* 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")}
|
||||
* (please note that when using a decorator, the class name of the mapper implementation ends with an underscore). To
|
||||
* inject that bean in your decorator, add the same annotation to the delegate field (e.g. by copy/pasting it from the
|
||||
* generated class):
|
||||
* and {@code @Singleton} (please note that when using a decorator, the class name of the mapper implementation ends
|
||||
* with an underscore). To inject that bean in your decorator, add the same annotation to the delegate field (e.g. by
|
||||
* copy/pasting it from the generated class):
|
||||
*
|
||||
* <pre>
|
||||
* public abstract class PersonMapperDecorator implements PersonMapper {
|
||||
|
@ -75,7 +75,7 @@ public @interface Mapper {
|
||||
* can be retrieved via {@code @Autowired}</li>
|
||||
* <li>
|
||||
* {@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>
|
||||
* The method overrides an unmappedTargetPolicy set in a central configuration set
|
||||
* by {@link #config() }
|
||||
|
@ -77,7 +77,7 @@ public @interface MapperConfig {
|
||||
* can be retrieved via {@code @Autowired}</li>
|
||||
* <li>
|
||||
* {@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>
|
||||
*
|
||||
* @return The component model for the generated mapper.
|
||||
|
@ -23,7 +23,9 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
@Named
|
||||
public class DateMapper {
|
||||
|
||||
|
@ -42,16 +42,16 @@ public class Jsr330ComponentProcessor extends AnnotationBasedComponentModelProce
|
||||
@Override
|
||||
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
||||
if ( mapper.getDecorator() == null ) {
|
||||
return Collections.singletonList( named() );
|
||||
return Arrays.asList( singleton(), named() );
|
||||
}
|
||||
else {
|
||||
return Collections.singletonList( namedDelegate( mapper ) );
|
||||
return Arrays.asList( singleton(), namedDelegate( mapper ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Annotation> getDecoratorAnnotations() {
|
||||
return Collections.singletonList( named() );
|
||||
return Arrays.asList( singleton(), named() );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -69,6 +69,10 @@ public class Jsr330ComponentProcessor extends AnnotationBasedComponentModelProce
|
||||
return true;
|
||||
}
|
||||
|
||||
private Annotation singleton() {
|
||||
return new Annotation( getTypeFactory().getType( "javax.inject.Singleton" ) );
|
||||
}
|
||||
|
||||
private Annotation named() {
|
||||
return new Annotation( getTypeFactory().getType( "javax.inject.Named" ) );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user