@Named

Making unused annotation types for each concrete usage doesn’t provide much value, as the sole reason for having such an annotation is to check the implementation class instance required by clients. To support such things, we have a built-in binding annotation called @Named, which takes a string. There's the method Names.named(), which returns @Named annotation when passing names as arguments:

bind(NotificationService.class).annotatedWith(Names.named("SMS"))          .to(SMSService.class);

We suggest utilizing @Named sparingly, because the compiler cannot check the string.

@Singleton is another useful annotation which we will discuss in detail in Chapter 5, Scopes.

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.