java - 在 Spring 中访问 MySQL 数据源 bean

标签 java spring jboss jndi

我正在尝试为MySQL创建一个数据源bean来 self 的Spring项目(在 springtoolsuite 中),我想要 访问MySQL JNDI (由 JBoss application server 运行)。

我的 Bean 声明

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" >
<property name="jndiName" value="java:/MySqlDS" />
<property name="resourceRef" value="true" />

从 JBoss 控制台:

java: Namespace
+- MySqlDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)

但是,当我运行测试应用程序时,我得到了以下结果。我做错了什么?

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in file [/workspace-sts/test1/cspringbean.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1401)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:540)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at com.don22.EscortIdol.main(EscortIdol.java:13)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398)
    ... 12 more

最佳答案

我还建议查看 JEE 命名空间,而不是定义工厂 bean

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>

参见http://static.springsource.org/spring/docs/2.5.6/reference/xsd-config.html

关于java - 在 Spring 中访问 MySQL 数据源 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2347392/

相关文章:

java - 使用 Spring 以非 root 用户身份在端口 80 上启动 bootRun 进程

spring - 如何使用 spring 注释生成 UUID

java - 热部署 Java EAR 以最小化或消除服务器上应用程序的停机时间?

java - mbeanServer.invoke 中的参数类型不匹配

java - Maven 过滤忽略一行中的第二个属性

java - 在哪里可以找到 Java 的最佳实践文档?

java - Spring 3.2.1 + hibernate-core 4.2.0 : merge() not working

java - G1GC 评论阶段花费的时间太长

java - 无法实例化接收器 GCMBroadcastReceiver

java - 如何将任何InputStream发送到JMS队列?