#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> * <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 {

View File

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

View File

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

View File

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

View File

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