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?
@Named
(javax.inject.Named) is equivalent of @Component
(org.springframework.stereotype.Component).
When used to annotated a class, it indicates that the class will be scanned and registered. If name is not given, DI framework will use the class type when injecting dependencies.
In short, you can't remove those @Named
annotation. If you do, everything will be compiled as normal. However, at runtime, you'll get runtime error something like cannot find bean xyz.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With