1

I've read a lot about switching between multiple datasource on runtime, but as far as I understand they're already defined datasources. I'm not quite sure on how can I just asign the datasources properties on runtime from a webservice call.

I don't need to switch between datasources, just need to create only one datasource with conection data coming from a webservice. Is there a way to retrieve these parameters from the webservice and create the datasource from that?

The policy here is to retrieve the datasource parameters from a webservice for all the projects, that way the connection data is not inside a file nor into the code, and is only manipulated by DBAs from a global security aplication.

I tried to call the web service in the same datasource file, but it didn't work.

Info:
Web service is a Soap
Web service Grails: 1.3.9

Regards.

4

1 回答 1

0

我认为您可以创建一个BeanPostProcessor来负责调用您的 Web 服务并更改数据源的设置。

可能您需要延迟创建会话工厂,确保 Grails 在您正确设置之前不会尝试使用您的数据源。

BeanPostProcessor 看起来像:

class WebserviceDataSourceBeanPostProcessor implements BeanPostProcessor {
    Object postProcessBeforeInitialization(Object bean, String beanName) {
        return bean
    }

    Object postProcessAfterInitialization(Object bean, String beanName) {

        if (bean instanceof DataSource){
           def info = //call webservice here...
           bean.username = info.username
           bean.password = info.password
           bean.url = info.url
           //checkout more setters in: http://commons.apache.org/proper/commons-dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html
        }

        return bean
    }
}

并确保你在resources.groovy中声明了这个 Spring Bean

beans = {
    webserviceDataSourceBeanPostProcessor(WebserviceDataSourceBeanPostProcessor)
}

如果您将有多个项目具有来自 Web 服务的相同配置,您可能会认为有可能为此使用插件,重用您的代码。

于 2013-05-30T17:25:51.737 回答