What is the best way to work with services in grails - 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 implementations - 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]

Related

equivalent of ActivatorUtilities.CreateInstance in Autofac

Are there any equivalent for following method from Microsoft Dependency Injection in Autofac.
ActivatorUtilities.CreateInstance(serviceProvider)
There is no direct analog for ActivatorUtilities in Autofac. But you have options.
You can directly resolve things that are registered (service location) - lifetimeScope.Resolve<T>()
If you need to resolve any type at all rather than only things you've registered, AnyConcreteTypeNotAlreadyRegisteredSource can help.
You can inject properties into constructed objects - lifetimeScope.InjectProperties(obj)
Or, if you really need ActivatorUtilities, you can use the Autofac.Extensions.DependencyInjection package to create a Microsoft container backed by Autofac and use the utility methods directly.

Grails: Register job from external library

I need to separate a grails service into a dedicated lilbrary in order to use this service across many applications. This works fine for the service itself as I am able to register the service bean in the resources.groovy (see https://docs.grails.org/latest/guide/spring.html).
This service happens to use a quartz job to get some functionality triggered regularly. So naturally I would move this job into the library and need to register it in the main application.
How can that be achieved? Thanks for you time!
Create grails plugin using this guide:
https://docs.grails.org/latest/guide/plugins.html
Then in src/main/groovy/YOUR_PACKAGE/YourPluginGrailsPlugin.groovy you can add bean configuration in doWithSpring() method (in same as resources.groovy way).

Hybris : How to create addons for acceleratorservices extension?

I am new to Hybris environment. i'm working on add-ons concept in Hybris. I can able to create addons for storefront but my question is how to create addons for acceleratorservices extension. I have tried the usual method but that is not working.
(ant addoninstall -Daddonnames="{addonName}" -DaddonStorefront.yacceleratorstorefront="acceleratorservices"). When i compile my system it is throwing cyclic reference error. So can anyone tell me like how to create addons for acceleratorservices extension.
Any sort answer is welcome from Hybris expert. Thanks in advance.
Addon is used to extends the functionnality of templates (extensions starting with y). In your case acceleratorservices is just a dependency of storefront. So you should just use spring to override/extends the features of acceleratorservices you need.
To do it well, you class must extends the one from acceleratorservices that you want to modify. Then depending on your needs, add or override methods.
Finally update your sping configuration with "aliases" to replace the bean reference by your new implementation.

How to use Grails Domain objects in a Java project?

I have a Grails application with outsourced (in a separate Grails Plugin) domain objects and would like to use the domain objects in a Java application.
If this is possible, how to do that?
Thanks
In order to use them as domain classes, you need GORM-plugin to be installed somehow in your java code. Without the plugin the domain classes lose their "active record" aspect and can be used only as POJOs.
I can barely imagine how to do that w/o lot of pain
You need to define your custom hbm.xml hibernate mapping files. See hibernate doc

Grails Plugin Development - override domain class

Plugins in Grails are great method to modularise an application.The documentation suggest to override the artifacts from the plugin in the application, which uses this plugin.
Is it realy the best approach?
Let's describe it by example: There is a domain class "org.User" defined in the plugin. The application overrides this domain class. If I use "grails run-app" then there are no warnings and it works. But Eclipse (GGTS) complains about "Invalid duplicate class definition of class org.User". For some developers it wouldn't matter, but I like the IDE helping on coding by stuf like "autocomplete".
At the end both classes are compiled an put on the java class loader. The application version of the class is loaded before the version of the plugin. The class resolver finds it first and that's why it works. Please correct me if I'm wrong at this point. Is it realy a good idea to have two versions of a class in one class loader?
What are the alternatives?
You can do like Spring Security Core plugin does, provide the User class as a template, so your application that use this plugin can choose between creating his own class or installing your default User class.
The plugin user template is here, and the script responsible to create this in the application is here.
You will need also a config value to know the class to use, and use it dynamic.
P.S: there are good security plugins like Shiro and Spring Security, maybe it's easier to check them instead of create your own.

Resources