Importing services in the Grails console? - grails

How do I get the services in the grails console? My business rules are implemented in services but I don't have access to them in the grails console. Does anyone know how to help me?

The Spring ApplicationContext is available as the ctx variable in the console, and you can use this to access Spring beans such as services. Typically that would be def myService = ctx.getBean('myService') but Grails adds a metaclass helper so you can just do def myService = ctx.myService

Related

GlassFish Metro: Dependency injection in JAX-WS

I require a stateful Web Service and I got it working so far using #HttpSessionScope. The service runs in a Servlet provided by the OSGi HttpService. This servlet is created by some Builder service in my OSGi environment. This builder has some services that need to be injected into my Web Service when it gets instantiated. I know that we have the #Inject and the #Resource annotations for this purpose, but I cannot find a way to add my external object to Glassfish Metro so that those objects get injected into my services.
Have a look to this example:
#HttpSessionScope
#WebService
public class AImpl implements A {
#Inject
private ADelegated delegated;
...
}
How can I declare an object of ADelegated to be injected into this Web Service? Is there some sort of ResourceInjector in Glassfish Metro that allows me to register an object for injection?

Java Config for Spring Web Service Marshallers

I have been trying to switch from XML configuration to Java configuration for Spring Web Services. The only element I can't seem to do is the marshallers.
The XML line:
annotation-driven marshaller="xmlBeansMarshaller" unmarshaller="xmlBeansMarshaller"
should be replaced with the Java class annotations:
#EnableWs
#Configuration,
but I can't seem to find how to setup the marshallers in code.
Any ideas?
Thanks
Marshallers can just be registered by using a regular #Bean definition. For instance, consider this sample app configuration.

Get SpringSecurityService in plugin

How I get the service springSecurityService bean in a own plugin?
I tried this, but i don't now if this is the correct strategy.
def doWithSpring = {
springSecurityService(application.mainContext.getBean('springSecurityService'))
}
If attempting to use it from a service or controller in the plugin, you can simply use
def springSecurityService
as a member variable in the groovy class. Since this is purely a runtime dependency, your plugin does not have to depend directly on the Spring Security Plugin. However, if your plugin is ever used in an application that does not make the Spring Security Plugin available at runtime, this will fail for obvious reasons.
Also important to note: this is true for any Grails service, not just the Spring Security Service.

Grails: Intercept the creation of a service from a plugin?

I have a plugin that's shared between multiple applications and I need to set some application specific options that will be used in a service in the plugin. It's an in house written plugin, however, the plugin has to be unaware of which application it's running in.
The best way I can think of doing this is to have some code run in the application right after the service is created in the plugin and call a method on the service to set the options. Is this possible?
If it's not possible, what other design could I implement to pass options to the plugin from the application.
Btw, these options need to be set when the application starts as well as throughout the running of the application.
It's best to use Bootstrap.groovy to to call a method(s) on a Grails service when an application starts. This service can be provided by the application or plugins in the application. Here is a quick example of how to do so:
Bootstrap.groovy
class BootStrap {
def myExampleService
def init = { servletContext ->
myExampleService.someMethodOnTheService()
}
}

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 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]

Resources