可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm developing an application using Spring. I'm required to use the @Service
annotation. I have ServiceI
and ServiceImpl
such that ServiceImpl implements ServiceI
. I'm confused here as to where should I keep the @Service
annotation.
Should I annotate the interface or the implementation with @Service
? What are the differences between these two approaches?
回答1:
I never put @Component
(or @Service
, ...) at an interface, because this make the interface useless. Let me explain why.
claim 1: If you have an interface then you want to use that interface for the injection point type.
claim 2: The purpose of an interface is that it define a contract that can been implemented by several implementations. On the other side you have your injection point (@Autowired
). Having just one interface and only one class that implement it, is (IMHO) useless, and violates YAGNI.
fact: When you put:
@Component
(or @Service
, ...) at an interface,
- have multiple classes that implements it,
- at least two classes become Spring Beans, and
- have an injection point that use the interface for type based injection,
then you will get and NoUniqueBeanDefinitionException
(or you have a very special configurations setup, with Environment, Profiles or Qualifiers ...)
Conclusion: If you use @Component
(or @Service
, ...) at an interface then you must violate at least one of the two clains. Therefore I think it is not useful (except some rare scenarios) to put @Component
at interface level.
Spring-Data-JPA Repository interfaces are something complete different
回答2:
Basically annotations like @Service, @Repository, @Component, etc. they all serve the same purpose:
auto-detection when using annotation-based configuration and classpath
scanning.
From my experience I am always using @Service
annotation on the interfaces or abstract classes and annotations like @Component
and @Repository
for their implementation. @Component
annotation I am using on those classes which serves basic purposes, simple Spring beans, nothing more. @Repository
annotation I am using in the DAO
layer, for e.g. if I have to communicate to the database, have some transactions, etc.
So I would suggest to annotate your interface with the @Service
and other layers depending on the functionality.
回答3:
I used @Component, @Service, @Controller and @Repository annotations only on the implementation classes and not on the interface. But @Autowired annotation with Interfaces still worked for me.
回答4:
Pros of putting annotation on @Service is that it gives a hint that it is a service. I don't know if any implementing class will by default inherit this annoation.
Con side is that you are coupling your interface with a specific framework i.e. Spring, by using spring specific annotation.
As interfaces are supposed to be decoupled from implementation, I would not suggest using any framework specific Annotations or object part of your interface.
回答5:
One benefit of spring is to easily switch Service (or other) implementation.
For this, you need to annotate on the interface and declare variable like this :
@Autowired
private MyInterface myVariable;
and not :
@Autowired
private MyClassImplementationWhichImplementsMyInterface myVariable;
Like the first case, you can activate which implementation to inject from the moment it is unique (only one class implements the interface).
In the second case, you need to refactor all your code (the new class implementation has another name).
As a consequence, the annotation needs to be on the interface as much as possible. Furthermore, JDK proxies are well suited for this : they are created and instantiated at application startup because runtime type is known by advance, contrary to CGlib proxies.
回答6:
I would put @Service on your class but put the name of the interface as a parameter to the annotation e.g.
interface ServiceOne {}
@Service("ServiceOne")
class ServiceOneImpl implements ServiceOne{}
By doing that you get all the benefits and can still inject the interface but get the class
@Autowired
private ServiceOne serviceOne;
So your interface is not tied to spring framework and you can change the class at any time and not have to update all your injection points.
So if I wanted to change the implementation class I could just annotate the new class and remove from the first but that's all that is required to be changed if you inject the class you could have a lot of work when ever you want to change the impl class
回答7:
To put it simply:
@Service is a Stereotype annotation for the service layer.
@Repository is a Stereotype annotation for the persistence layer.
@Component is a generic stereotype annotation used to tell Spring to create an instance of the object in the Application Context. It's possible to
define any name for the instance, the default is the class name as camel case.
回答8:
There are 5 annotations which could be used for making spring beans. List in below of answers.
Do you really need an interface? If you are going to have one implementation for each service interface, just avoid it, use only class. Of course, if you don't have RMI or when interface proxy is required.
@Repository - use for injecting your dao layer classes.
@Service - use for injecting your service layer classes. In service layer also you might need to use @Transactional annotation for db transaction management.
@Controller - use for your frontend layer controllers, such as JSF managed beans injecting as spring beans.
@RestController - use for spring rest controllers, this would help you to avoid every time to put @ResponseBody and @RequestBody annotations in your rest methods.
@Component - use it in any other case when you need to Inject spring bean which is not controller, service, or dao class