A tonne of code at my company uses the javax.inject.Named annotation with the default value, which the Javadoc indicates is the empty string "".
For example:
@Named
public class Foo {
...
}
This does not appear to add any value, since the empty string doesn't have any semantic meaning. If I remove the @Named annotations will there be any harmful effects?
The question What is javax.inject.Named annotation supposed to be used for? describes how @Named functions, but doesn't explain any special significance of the empty string, or why it would be necessary or beneficial to omit the actual name.
The question When should you explicitly name a Managed Bean? likewise talks about when you would want to use names to differentiate injectable beans, but doesn't provide any rationale for the use of the empty string as a name.
Can I delete these un-named @Named annotations without breaking anything?
It's impossible to know if you will break anything without analyzing all the code that constructs injection keys and all the code that injects any of these bindings.
In some JSR-330 implementations (e.g. Dagger) it's not possible to use a
@Namedannotation with a value constructed at runtime, but in other implementations (e.g. Guice) it is possible and in fact commonly done.For example, I could imagine a Guice module like:
This provides a binding for an unannotated
Foowhich delegates to a@Named(x) Foo, wherexis determined by a constructor argument to the module -- which could be constructed at runtime, or derived from some default somewhere, etc.You could imagine code building an injector like:
Where
getSelectedFooConfig()might return""as a default or fallback.In a situation like that,
@Namedwithout any name could be a reasonable fallback value to use. If your application is doing anything like that, then it is not safe to remove the@Namedbindings, because an un-annotated binding is not equivalent to a binding with an empty string.I still would argue that this is not a good design: it would be better to use a dedicated qualifier annotation for this purpose (e.g.
@ConfigBased("foo-config")) rather than just using@Named. If you were doing that then you could at least identify which strings were being used (or, better yet, eschew strings and use an enum instead).