Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

java - Both @Component and @Named for the same bean class

Does a class which will act as a bean in a Spring application require both @Component and @Named at the same time?

What is the significance if both are used so?

I tried searching the net as well as saw the standard documentation of these annotations and found them a bit confusing.

Finally which name is taken by the application if the @Named annotation does not specify any name for the bean?

question from:https://stackoverflow.com/questions/10189531/both-component-and-named-for-the-same-bean-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@Component and @Named are annotations that basically do the same thing, but come from different APIs.

@Component belongs to Spring API. It marks class to be autodetected as a bean and optionally allows you to specify a name for that bean (@Component("foo")). Without explicit name specification detected bean will get a default name derived from the name of its class.

@Named belongs to javax.inject API. It marks class to be autodetected as a bean and requires you to specify a name.

Spring supports both these APIs. It doesn't make sense to use both annotations at the same class since they provide the same functionality.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...