0

I'm new to annotations and am wondering if I need the @Named annotation for my Bar class. I've googled some but still don't know if the annoation is needed for this inner class.

public abstract class Foo{
         //code
     @Named
     @Configuration
     class Bar{
         //code
    }
}
John Earl
  • 95
  • 2
  • 8

1 Answers1

1

In vanilla javax.inject.Named, as per docs, the annotation is intended to define a String based qualifier. If you don't intend to provide a qualifier, you don't need to put the annotation. :-)

In Spring though, the docs state that you can provide a @Named annotation without a value, with the same practical effect of using @Component. I wouldn't recommend that as the code looks misleading.

everton
  • 7,579
  • 2
  • 29
  • 42
  • forgot to mention I'm using spring as well. So the way it was explained to me in that context is that @Named in spring will still work on classes without a qualifier. Not sure if this changes the answer at all. – John Earl Sep 13 '18 at 03:17
  • 1
    @JohnEarl as per https://stackoverflow.com/questions/18540696/named-annotation-in-spring-mvc it seems that you can indeed use it, without a qualifier, instead of `@Component`. – everton Sep 13 '18 at 03:48
  • Updated the answer with the spring information we discovered – everton Sep 13 '18 at 19:01
  • 1
    Thanks, very helpful answer. – John Earl Sep 13 '18 at 19:16