问题
In Grails 3.3.3, when I run generate-all
for a domain class, a Service Interface is generates (versus the actual Service Class from Grails 2.x). I actually didn't notice it until I tried to add a method to my service.
The interface gets placed in the services folder where the service would live. I actually do like the interface but I still want the service and the default implementations. How can I have both an interface and implementation live in the services folder if the interface already has the name of the service? (Ex the interface gets named ClientService.groovy
so the implementation would have the same name)
Here is an example of an interface that get's generated
package project
import grails.gorm.services.Service
@Service(Client)
interface ClientService {
Client get(Serializable id)
List<Client> list(Map args)
Long count()
void delete(Serializable id)
Client save(Client client)
}
回答1:
How can I have both an interface and implementation live in the services folder if the interface already has the name of the service?
If you have an interface marked with @Service
then you shouldn't have an implementation source file at all. GORM Data Services generates the implementation for you at compile time. If you want to write some of the code yourself then instead of an interface write an abstract class and annotate it with @Service
. There is no reason for you to have an interface marked with @Service
and then write a class that implements that interface.
来源:https://stackoverflow.com/questions/49326761/grails-3-3-3-generate-all-domain-class-creates-only-the-service-interface