简体   繁体   中英

Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name defined in class path resource

There was a mySql DB connection in my application, i need to crate another connection for different mySql DB for that i replicate the already created connection's steps with some different name in order to create the new mySQL DB connection but im facing the following exception, can any one help me by giving some hint where did i make the mistake.

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

Here are code chunks.

in java file where i need to use the DB Object:

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

Application-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>

Here is .properties file details having DB details:

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 

EDIT dataSourcePivotDBJNDI

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

I would focus on this line:

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

Probably you are missing JNDI name definition in the server.xml or somewhere else ...

I review all the files on server and found there was a file at location

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

in the file all the jnid names were mentioned i also added the name and value like

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

and it worked, Thanks for all who helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Error org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource ERROR org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource Springboot:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'showAuthorsView' defined in class path resource org.springframework.beans.factory.BeanCreationException: Error creating bean with name XXX defined in class path resource org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webHandler' defined in class path resource org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptDataSourceInitializer' defined in class path resources? org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [jlcindia.xml] Spring mvc + hibernate Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM