GrailsContext Application Update

In my spring / resources.xml configuration file, I define a bean as follows:

<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://${remote.host}:8080/MyAgent/remoting/MyService"/>
    <property name="serviceInterface" value="services.MyService"/>
</bean>

In my Config.groovy file I have: remote.host = "someipaddress"

Now I want to change this placeholder value at runtime. In a regular spring application, I do this through PropertyPlaceHolderConfigurer, then update the context and it works.

In Grails, how can I update the context?

Hi,

Philip

+3
source share
5 answers

Well, I am refusing a refreshing approach. As a workaround, I created a grails service that looks like this:

class myService {
    def myRemoteService
    static transactional = false

    private MyRemoteService getService(String remoteServiceURL) {
        HessianProxyFactory factory = new HessianProxyFactory();
        try {
            return (MyRemoteService) factory.create(MyRemoteService.class, url);
        }
        catch (MalformedURLException e) {
            e.printStackTrace()
        }
        return null
    }

    def someRemoteMethod(String remoteServiceURL) {
        getService(remoteServiceURL).myRemoteMethod()
    }
}

This allows me to call a remote service on any remote machine dynamically.

, : -S

+1

:

def blabla
...
void someServiceMethod() {
   blabla.someProperty = 'new value'
}

def blabla
...
def someControllerAction = {
   blabla.someProperty = 'new value'
}
0

grailsApplication refresh(), , spring, .

0

Grails , grails app-context.

InitializingBean .

import org.springframework.beans.factory.InitializingBean

class ExampleService implements InitializingBean { 

   def grailsApplication 
   def setting

   void afterPropertiesSet() { 
      this.setting = grailsApplication.config.setting 
   }
}

, , , , .

0

, :

import grails.spring.BeanBuilder

def bb = new BeanBuilder(
        application.parentContext,
        new GroovyClassLoader(application.classLoader))
def beans = bb.beans {
    myService(org.springframework.remoting.caucho.HessianProxyFactoryBean) {
        ...
    }
}
beans.registerBeans(application.mainContext)

This is pretty much what plugins do when they need to swap new bean instances. You can also raise the JIRA problem for a more convenient way to do this.

0
source

Source: https://habr.com/ru/post/1776946/


All Articles