将Xml配置转换为resource.groovy

时间:2014-10-30 16:06:23

标签: grails groovy

我是grails的新手,我想将xml配置转换为resource.groovy。但是xml中有名称空间。我不想在这里复制配置。应该有一个配置是resource.groovy

我的xml是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
            http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

            <context:component-scan base-package="neo4j"></context:component-scan>


    <util:map id="config">
        <entry key="ha.server_id" value="2" />
        <entry key="ha.initial_hosts" value="127.0.0.1:5001,127.0.0.1:5002" />
        <!-- put in more config parameters here, http://docs.neo4j.org/chunked/stable/ha-configuration.html -->
    </util:map>
    <bean id="graphDbFactory"
        class="org.neo4j.graphdb.factory.HighlyAvailableGraphDatabaseFactory" />
    <bean id="graphDbBuilder" factory-bean="graphDbFactory"
        factory-method="newHighlyAvailableDatabaseBuilder">
        <constructor-arg value="/home/alok/Desktop/data4" />
    </bean>
    <bean id="graphDbBuilderFinal" factory-bean="graphDbBuilder"
        factory-method="setConfig">
        <constructor-arg ref="config" />
    </bean>
    <bean id="graphDatabaseService" factory-bean="graphDbBuilderFinal"
        factory-method="newGraphDatabase" destroy-method="shutdown" />

    <neo4j:config graphDatabaseService="graphDatabaseService"   base-package="neo4j"/>
    <neo4j:repositories base-package="neo4j" />

</beans>

我想知道如何处理命名空间和构造函数参数

1 个答案:

答案 0 :(得分:6)

这里你真的有两个问题,但让我们回答它们。

如何在resources.groovy中使用名称空间?

以下是:

// resources.groovy
beans {
  xmlns neo4j:"http://www.springframework.org/schema/data/neo4j"
  xmlns context:"http://www.springframework.org/schema/context"
  ... // and so on.
  // neo4j.repositories
}

如何在resources.groovy中使用构造函数参数?

以下是:

// resources.groovy
beans {
  someBean(
    SomeBeanClass, 
    'stringArg1', 
    ['listOfStringsArg2', 'listOfStringArg2-a'],
    ref('someOtherBeanAsArg3')
  )
}