2010-11-25 72 views
3

在我的配置的春/ resources.xml中的文件,我這樣定義一個bean:清爽的Grails的applicationContext

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

在我的Config.groovy文件我有: remote.host = 「someipaddress」

現在我想在運行時更改此佔位符的值。在一個普通的春季應用程序中,我通過PropertyPlaceHolderConfigurer來完成此操作,然後刷新上下文並使其工作。

在Grails中,我該如何刷新上下文?

問候,

菲利普

+0

如果我沒有得到你錯了,你問如何刷新上下文(您可以隨時重新啓動服務器或重新部署應用程序 - 因爲您不需要每天更改設置),但主要問題是如何在不重新編譯的情況下替換remote.host。 - 那是對的嗎? – robbbert 2010-11-27 13:13:33

+0

不是......它會隨時隨地動態替換remote.host,因爲我將連接到託管在不同主機上的多個遠程服務。 (是的,我知道maybie這不應該是一個單身人士,但這並不能解決我的刷新問題......) – Philippe 2010-11-27 15:14:53

回答

1

好吧,我放棄了令人耳目一新的方法。 作爲一種變通方法,我創建了一個Grails的服務,看起來像這樣:

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() 
    } 
} 

這樣我就可以調用遠程服務的任何遠程的機器上dinamically。

我仍然有興趣在一個清潔的解決方案,因爲這讓我改寫爲每個遠程方法的包裝方法:-S

0

爲什麼不直接更新值:

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

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

這是真的,但在我的情況下,該bean實際上是一個動態代理(org.springframework.aop.framework。 JdkDynamicAopProxy),所以我沒有直接訪問代理接口的字段。 – Philippe 2010-11-26 10:33:58

0

grailsApplication暴露刷新()方法,我不是確定它是否會重新加載spring context,你可以試試。

+0

那麼,grailsApplication。mainContext應該是可刷新的,但是我得到這個:2010-11-26 12:10:27,841 [http-8080-1] ERROR errors.GrailsExceptionResolver - GenericApplicationContext不支持多次刷新嘗試:只調用'refresh'一次 java.lang .IllegalStateException:GenericApplicationContext不支持多刷新嘗試,只要調用: '刷新' 一次 \t在ServiceUrlFilters $ _closure1_closure3_closure5.doCall(ServiceUrlFilters:33) \t在java.lang.Thread.run(Thread.java:680) – Philippe 2010-11-26 11:12:01

0

我在grails郵件列表中做了一個快速搜索,看起來像grails不支持app-context重載。

你可以嘗試實現InitializingBean並從app config直接獲取值。

import org.springframework.beans.factory.InitializingBean 

class ExampleService implements InitializingBean { 

    def grailsApplication 
    def setting 

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

也許你可以聽在配置改變或每次你需要使用它的時候獲得的財產,我不知道,我不能創建一個應用程序,現在運行一些測試。

+0

謝謝,但我的服務沒有定義爲我可以在我的應用程序中控制的Grails服務。這是一個部署在遠程tomcat上的java服務,通過Hessian公開。在我的grails應用程序中,我只有服務接口,並通過HessianProxyFactoryBean配置了對遠程服務的訪問...... – Philippe 2010-11-26 15:14:41

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) 

這是相當多的,當他們需要在新的bean實例換什麼插件做。你也可以提出一個更好的方法來解決JIRA問題。