【问题标题】:How convert <jee:jndi-lookup> to Spring JavaConfig in a Websphere Environment如何在 Websphere 环境中将 <jee:jndi-lookup> 转换为 Spring JavaConfig
【发布时间】:2016-05-04 15:17:42
【问题描述】:

我有一个旧的 Spring xml 配置。

<jee:jndi-lookup jndi-name="ree/configuration" cache="true" id="re-properties-config" />

在 WebSphere 8 中,我在 JNDI 名称“ree/configuration”下有一个“资源环境提供者”和一个“资源环境条目”。 Referenceables 类是 java.util.Properties。

使用 xml 配置一切正常。来自 WebSphere 的“资源环境条目”映射到属性对象中。

现在我想迁移到 Spring JavaConfig。 什么是最好的解决方案?

我试试这个:

private static Properties jndiProperties() {
    Properties properties = null;
    JndiTemplate jndi = new JndiTemplate();
    try {
        properties = (Properties)jndi.lookup("ree/configuration");
        LOG.info("JNDI Properties loaded: " + properties);
    } catch (NamingException e) {
        LOG.error("NamingException for ree/configuration", e);
    }
    return properties;
}

但它因 ClassCastException 而失败。 javax.naming.Reference 不能转换为 java.util.Properties

【问题讨论】:

    标签: java spring websphere jndi


    【解决方案1】:

    您可以尝试使用此代码从返回的参考中获取属性:

                Reference ref = jndi.lookup("ree/configuration");
                Enumeration e = ref.getAll();
                while( e.hasMoreElements() )
                {
                   RefAddr ra = (RefAddr) e.nextElement();
                   properties.put( ra.getType().toLowerCase(), ra.getContent() );
                }
    

    有关如何使用参考的更多示例,请尝试此链接:http://www.programcreek.com/java-api-examples/javax.naming.Reference

    【讨论】:

    • 我也找到了这个解决方案。我认为有更舒服的东西:-)
    • 好的。很高兴知道建议的答案实际上对您有用。
    【解决方案2】:

    see相关。

    您可以定义一个@Configuration 类并像这样创建一个bean。

    @Bean(name = "dbDataSource")
    public DataSource dataSource(@Value("${db.jndi}" String jndiName) {
        JndiDataSourceLookup lookup = new JndiDataSourceLookup();
        return lookup.getDataSource(jndiName);
    }
    

    您也可以从属性中提取JNDI,而不将其作为输入参数接收,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 2017-11-04
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多