#664 Annotate mapper implementations with @Singleton when using jsr330.

This commit is contained in:
Sam Wright 2015-10-20 17:49:46 +01:00 committed by Andreas Gudian
parent f0559fca43
commit 5ede0e91db
5 changed files with 14 additions and 8 deletions

View File

@ -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 {

View File

@ -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() }

View File

@ -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.

View File

@ -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 {

View File

@ -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" ) );
}