【问题标题】:How do I write the equivalent of Spring's jee:jndi-lookup in Grails resources.groovy?如何在 Grails resources.groovy 中编写相当于 Spring 的 jee:jndi-lookup 的内容?
【发布时间】:2011-11-04 14:33:58
【问题描述】:

我想采用在 Grails 的 resource.xml(标准 Spring xml 内容)中可以正常工作的类似内容:

    <jee:jndi-lookup id="remoteConnectionFactory"
    jndi-name="jms/WLQueueConnectionFactory" resource-ref="false">
       <jee:environment>
           java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
           java.naming.provider.url=t3://remote_uri:port/
       </jee:environment>
 </jee:jndi-lookup>

并将其转换为 resources.groovy 中的 Grails bean builder DSL。我已经尝试了以下组合(基本上是反复试验,看看我是否可以让它工作,没有一个):

ejbJndi(JndiTemplate)
{ bean ->
    bean.scope = 'session'
    environment = [
        "java.naming.provider.url" : "t3://remote_uri:port/",
        "java.naming.factory.initial" : "weblogic.jndi.WLInitialContextFactory"
    ]
}
xmlns jee:"http://www.springframework.org/schema/jee"
xmlns context:"http://www.springframework.org/schema/context"

jee.'jndi-lookup'(id:"jmsConnectionFactory", jndiName: "com.retailexp.jms.ConnectionFactory", lookupOnStartup: false,
    proxyInterface: "javax.jms.ConnectionFactory", resourceRef: "false", 'jndi-environment': ref("ejbJndi")) {
    cache = true
    exposeAccessContext = true

    jndiTemplate = ref("ejbJndi")
    jndiEnvironment = [
        "java.naming.provider.url" : "t3://remote_uri:port/",
        "java.naming.factory.initial" : "weblogic.jndi.WLInitialContextFactory"
    ] as Properties

    environmentRef = [
        "java.naming.provider.url" : "t3://remote_uri:port/",
        "java.naming.factory.initial" : "weblogic.jndi.WLInitialContextFactory"
    ]

    environment = """
        java.naming.provider.url=t3://remote_uri:port/
        java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
"""

同样,这些是我尝试过的各种组合,而不是最终代码的样子(我希望其中只有一个可以工作!)。正如你所看到的,我有点在黑暗中摸索。

jndi-lookup 设置正常并且是有效的 bean DSL,但环境(JndiTemplate 类型的东西)没有我设置为获取远程 InitialContext 的值(java.naming.factory.initial 等)。

【问题讨论】:

    标签: spring grails


    【解决方案1】:

    如果您想使用 XML 命名空间,您需要在 Grails 'beans' 定义的顶部添加以下内容:

    beans {
        xmlns context:"http://www.springframework.org/schema/context"
        xmlns jee:"http://www.springframework.org/schema/jee"
    
        context.'property-placeholder'('location':'classpath:config.properties')
        jee.'jndi-lookup'(id:"jmsConnectionFactory", jndiName: "com.retailexp.jms.ConnectionFactory", lookupOnStartup: false, etc.
    
        ...
    
    }
    

    或者您可以尝试使用 'bean' 表示法,而不是尝试使用 XML 命名空间 'jee...',这将更冗长但更容易在 Grails bean 表示法中实现。

    XML:

    <bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/MyDataSource"/>
        <property name="jndiEnvironment">
            <props>
                <prop key="foo">bar</prop>
            </props>
        </property>
    </bean>
    

    Grails DSL:

    beans {
        simple(org.springframework.jndi.JndiObjectFactoryBean) {
            jndiName = 'jdbc/MyDataSource'
            jndiEnvironment = ["foo":"bar"]
        }
    
        ...
    }
    

    有关更多信息,请参阅 Grails 文档“使用 Spring 命名空间”here

    我还 blogged 介绍了在 Grails 之外使用 Grails BeanBuilder DSL,这可能会有所帮助,但 Grails 文档很可能会更有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      相关资源
      最近更新 更多