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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.154