Reputation: 22357
There is a lot of boiler plate code when developing Java EE apps.
One of the places is the need for interfaces for Services. Is it possible to skip the interface part.
I am asking since the @Service annotation is used on a class, and not the interface.
@Repository is for instance used on the interface.
What would be the implications of not using an interface for a service? Will @Transaction and other annotations work as expected?
I do understand Grails and other Frameworks don't have the need for interfaces so although you'd say it is good for business logic and what not, I understand this aspect but it is not always necessary and can be added afterwards, when there is a need.
Upvotes: 0
Views: 1192
Reputation: 7126
There's no real danger in not using an interface for your service. Not using one just means your code is deeply tied to the service implementation. In practice, I don't create an interface for my services. I just annotate my concrete class with Service because it's rare that I need a generic service class that has several implementations. A repository on the other hand, you will often see an interface annotated with Repository because Spring Data uses AOP to create an instance of your repository at runtime. What you could do if you want to create an interface is use java bean config. Create a bean method that instantiates a concrete service class, the return type could be the interface your service class implements. That way your concrete implementation is only tied to the initial bean config and you can change it out pretty easily.
Upvotes: 1