以编程方式为Spring创建JNDI DataSource

编程入门 行业动态 更新时间:2024-10-13 12:23:02
本文介绍了以编程方式为Spring创建JNDI DataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have an existing Spring web-based application that has datasources defined using JNDI, and I'm trying to create a standalone app to use the beans. How can I create the JNDI entry and database properties programmatically in the standalone application?

<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:需要在环境或系统属性中指定类名

I tried something like this, but am now getting the error "javax.naming.NoInitialContextException: Need to specify class name in environment or system property"

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(); } }

推荐答案

org.springframework.test 依赖项通过 SimpleNamingContextBuilder :

The org.springframework.test dependency has support for that via the 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( "..." );

这应该有效。

干杯,

更多推荐

以编程方式为Spring创建JNDI DataSource

本文发布于:2023-10-26 18:25:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   Spring   DataSource   JNDI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!