上下文初始化失败org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名称创建bean时出错

时间:2014-09-01 08:06:07

标签: java spring jdbc

我的应用程序中有一个mySql数据库连接,我需要为不同的mySql数据库创建另一个连接,因为我用一些不同的名称复制已经创建的连接的步骤,以便创建新的mySQL数据库连接,但即时面对以下例外,任何人都可以通过给出一些提示帮助我,我在哪里犯了错误。

01 Sep 2014 01:34:41,931 ERROR [localhost-startStop-1] context.ContextLoader:227 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplatePivotDB' defined in class path resource [application-context-dao.xml]: Cannot resolve reference to bean 'dataSourcePivotDBJNDI' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourcePivotDBJNDI' defined in class path resource [application-context-dao.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [${jdbc.pivotDB.jndi.name}] is not bound in this Context. Unable to find [${jdbc.pivotDB.jndi.name}].
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:616)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:650)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1582)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourcePivotDBJNDI' defined in class path resource [application-context-dao.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [${jdbc.pivotDB.jndi.name}] is not bound in this Context. Unable to find [${jdbc.pivotDB.jndi.name}].
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 31 more
Caused by: javax.naming.NameNotFoundException: Name [${jdbc.pivotDB.jndi.name}] is not bound in this Context. Unable to find [${jdbc.pivotDB.jndi.name}].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
    at javax.naming.InitialContext.lookup(Unknown Source)
    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:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 38 more

以下是代码块。

在我需要使用DB对象的java文件中:

@Resource(name = "jdbcTemplatePivotDB")
    private JdbcTemplate jdbcTemplatePivotDB;

应用程序的context.xml

  <bean id="jdbcTemplatePivotDB" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg index="0">
                <ref bean="dataSourcePivotDB${jdbc.ds.type}" />
            </constructor-arg>
        </bean>

<bean id="txManagerPivotDB"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourcePivotDB${jdbc.ds.type}" />
    </bean>

<bean id="dataSourcePivotDBCustom" class="org.apache.commons.dbcp.BasicDataSource"
        lazy-init="true">
        <property name="driverClassName" value="${jdbc.pivotDB.driverClassName}" />
        <property name="url" value="${jdbc.pivotDB.url}" />
        <property name="username" value="${jdbc.pivotDB.username}" />
        <property name="password" value="${jdbc.pivotDB.password}" />
        <property name="maxActive" value="${jdbc.pool.maxActive}" />
        <property name="maxIdle" value="${jdbc.pool.maxIdle}" />
        <property name="validationQuery" value="${jdbc.pool.validate}" />
    </bean>

这是具有数据库详细信息的.properties文件详细信息:

jdbc.pivotDB.driverClassName=com.mysql.jdbc.Driver
jdbc.pivotDB.username=pivotuser
jdbc.pivotDB.password=pivotPass
jdbc.pivotDB.url=jdbc:mysql://ServerIP:3306/DBName

jdbc.pool.maxActive=10
jdbc.pool.maxIdle=10
jdbc.pool.validate=SELECT 1 

编辑dataSourcePivotDBJNDI

<bean id="dataSourcePivotDBJNDI" class="org.springframework.jndi.JndiObjectFactoryBean"
        lazy-init="true">
        <property name="jndiName" value="${jdbc.pivotDB.jndi.name}" />
    </bean>

2 个答案:

答案 0 :(得分:3)

我会专注于这一行:

Caused by: javax.naming.NameNotFoundException: Name [${jdbc.pivotDB.jndi.name}] 
is not bound in this Context. Unable to find [${jdbc.pivotDB.jndi.name}].

可能你在server.xml或其他地方缺少JNDI名称定义......

答案 1 :(得分:1)

我查看了服务器上的所有文件,发现位置上有一个文件

conf --> applicationNameDir --> AppName-app.properties 

在文件中提到了所有jnid名称,我还添加了名称和值,如

jdbc.pivotDB.jndi.name=java:comp/env/jdbc/pivotDB

它有效,感谢所有帮助过我的人。