【问题标题】:Programmatically creating a JNDI DataSource for Spring以编程方式为 Spring 创建 JNDI 数据源
【发布时间】:2013-02-05 14:07:25
【问题描述】:

我有一个现有的基于 Spring Web 的应用程序,它具有使用 JNDI 定义的数据源,我正在尝试创建一个独立的应用程序来使用 bean。如何在独立应用程序中以编程方式创建 JNDI 条目和数据库属性?

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/MyDS" />
</bean>

    public static void main(String[] args) {

      // this throws an error since the JNDI lookup fails - can I programmatically define the database properties here?

    ClassPathXmlApplicationContext ctx = new  ClassPathXmlApplicationContext("applicationContext.xml");

    UserService userService = ctx.getBean(UserService.class);
    User user = userService.findUserById("jdoe");

    System.out.println("display name: " + user.getDisplayName());
}

编辑:

我尝试了类似的方法,但现在收到错误“javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名”

public static void main(String[] args) {
    setupJNDI();

    ClassPathXmlApplicationContext ctx = new  ClassPathXmlApplicationContext("applicationContext.xml");

    UserService userService = ctx.getBean(UserService.class);
    User user = userService.findUserById("jdoe");

    System.out.println("display name: " + user.getDisplayName());
}


private static void setupJNDI() {
    InitialContext ic;
    try {
        ic = new InitialContext();
        ic.createSubcontext("java:");
        ic.createSubcontext("java:/comp");
        ic.createSubcontext("java:/comp/env");
        ic.createSubcontext("java:/comp/env/jdbc");
        SQLServerConnectionPoolDataSource myDS = new SQLServerConnectionPoolDataSource();
        opaDS.setServerName("myserver");
        opaDS.setPortNumber(1433);
        opaDS.setUser("user");
        opaDS.setPassword("password");

        ic.bind("java:/comp/env/jdbc/MyDS", myDS);
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java spring mocking jndi initial-context


    【解决方案1】:

    org.springframework.test 依赖项通过 SimpleNamingContextBuilder 提供支持:

    // First create the mock JNDI tree and bind the DS
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    DataSource ds = new ComboPooledDataSource();
    ds.setDriverClass( ... ); // etc. for uid, password, url
    builder.bind( "java:comp/env/jdbc/MyDS" , ds );
    builder.activate();
    
    // Then create the Spring context, which should now be able 
    // to resolve the JNDI datasource
    ApplicationContext context = new ClassPathXmlApplicationContext( "..." );
    

    应该可以的。

    干杯,

    【讨论】:

    • 我试过这个,但我得到错误“线程“主”org.springframework.beans.factory.BeanCreationException中的异常:创建类路径资源中定义的名称为“myDataSource”的bean时出错[applicationContext.xml ]: init 方法调用失败;嵌套异常为 javax.naming.NoInitialContextException: 需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中:java.naming.factory.initial'
    • 我的错,JNDI 树当然应该在尝试创建 Spring 上下文之前创建和激活 - 我已经相应地编辑了答案。
    • 您能否给我一个合理的解释,为什么这个解决方案在使用 jtaDataSource 时会出现错误?
    猜你喜欢
    • 1970-01-01
    • 2017-04-07
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多