gpt4 book ai didi

java - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException : Error creating bean with name defined in class path resource

转载 作者:行者123 更新时间:2023-11-29 03:17:37 26 4
gpt4 key购买 nike

我的应用程序中有一个 mySql DB 连接,我需要为不同的 mySql DB 创建另一个连接,为此我用一些不同的名称复制了已经创建的连接的步骤,以创建新的 mySQL DB 连接,但我面临以下问题异常,任何人都可以通过提示我在哪里犯了错误来帮助我。

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;

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>

这是包含数据库详细信息的 .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>

最佳答案

我会关注这一行:

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 名称定义 ...

关于java - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException : Error creating bean with name defined in class path resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com