开发者

What is the best way to work with services in grails

I've just started a project on grails and didn't find how to work with services using dependency injection and interfaces.

As I've seen so far in the documentation when you create a service - it's just a groovy class which can be auto wired wherever I want.

But what if I want to have an interface for a service and to inject one of its implementation like I did in Java using spring?

eg I want to have a service interface. let it be MyService.groovy it will have 1 method doSmth() and I'll have 2 imple开发者_StackOverflowmentations - MyServiceImpl1.groovy and MyServiceImpl2.groovy

I have a quartz job doing something like this def myService myService.doSmth()

Where should I put groovy interface (folder)? Shall I create a package for that in src/groovy? How to configure resources.groovy to wire "myService" with 1 of the service implementation?

Any thoughts are appreciated

Thanks in advance!


Running grails create-service [name] is a convenient way of get a service deployed, but it doesn't create an interface with implementation, as you're looking for.

I'd suggest putting your interface and implementations into src/groovy and using resources.groovy to wire them up (you can access the environment, if you want to deploy a different implementation by environment).

Take a look at the 'Using the Spring DSL' section in chapter 14.2 of the user guide for how to wire up your service in resources.groovy. You also have the option of using resources.xml if you want to wire with XML, but I'd definitely recommend the Groovy DSL.


Just run grails create-service [name]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜