Bean 必须是 'javax.sql.DataSource' 类型,Java Spring 除外
Bean must be of 'javax.sql.DataSource' type, exception with Java Spring
我刚刚复制了一个项目,当我编译时我的 applicationContent.xml 出现错误,当我将鼠标放在 bean 'entity' 中的红色 ='dataSource' 我得到错误 “Bean 必须是 'javax.sql.DataSource' 类型,异常“ 我正在尝试找到如何让它工作,但我做不到。
日志错误说:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entity' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: El nombre jdbc no este asociado a este contexto
当然还有我的 applicationContext.xml 文件:
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='http://www.springframework.org/schema/beans'
xmlns:jee='http://www.springframework.org/schema/jee'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:tx='http://www.springframework.org/schema/tx'
xmlns:p='http://www.springframework.org/schema/p'
xmlns:context='http://www.springframework.org/schema/context'
xsi:schemaLocation='http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd'>
<bean id='velocityEngine' class='org.springframework.ui.velocity.VelocityEngineFactoryBean'>
<property name='velocityProperties'><value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value></property></bean>
<bean id='dataSourceRemote' class='org.springframework.jdbc.datasource.DriverManagerDataSource'
p:driverClassName='com.mysql.jdbc.Driver' p:url='jdbc:mysql://172.23.23.148/SIWP' p:username='webpil' p:password='s2i0w1p0pilandina'/>
<bean id='entity' class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
<property name='dataSource' ref='dataSource'/>
<property name="jpaProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
</props>
</property>
</bean>
<bean id='transaction' class='org.springframework.orm.jpa.JpaTransactionManager' p:entityManagerFactory-ref='entity'/>
<bean id='appContextAware' class='com.pil.sile.core.view.util.AppContextAware'/>
<tx:annotation-driven transaction-manager='transaction'/>
<jee:jndi-lookup id='dataSource' jndi-name='jdbc/sile'/>
<context:annotation-config/>
</beans>
也许你可以告诉我如何定义那个 bean 类型,或者我做错了什么。欢迎任何帮助
我假设您正在使用 Tomcat 服务器。
在您的 applicationContext.xml 中,它正在尝试查找容器提供的 JNDI 数据源。
您需要在 tomcat 的 server.xml 中声明 JNDI 资源,如下所示。
提供数据库相关配置,如url、用户名、密码、驱动程序class等
<GlobalNamingResources>
<Resource name="jdbc/sile"
auth="Container"
type="javax.sql.DataSource"
username="dbUser"
password="dbPassword"
url="jdbc:postgresql://localhost/dbname"
driverClassName="org.postgresql.Driver"
initialSize="20"
maxWaitMillis="15000"
maxTotal="75"
maxIdle="20"
maxAge="7200000"
testOnBorrow="true"
validationQuery="select 1" />
</GlobalNamingResources>
下一步是从 Tomcat 的 Web context.xml
引用创建的 JNDI 资源
<ResourceLink name="jdbc/DatabaseName"
global="jdbc/sile"
type="javax.sql.DataSource"/>
参考文档:
我刚刚复制了一个项目,当我编译时我的 applicationContent.xml 出现错误,当我将鼠标放在 bean 'entity' 中的红色 ='dataSource' 我得到错误 “Bean 必须是 'javax.sql.DataSource' 类型,异常“ 我正在尝试找到如何让它工作,但我做不到。
日志错误说:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entity' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: El nombre jdbc no este asociado a este contexto
当然还有我的 applicationContext.xml 文件:
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='http://www.springframework.org/schema/beans'
xmlns:jee='http://www.springframework.org/schema/jee'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:tx='http://www.springframework.org/schema/tx'
xmlns:p='http://www.springframework.org/schema/p'
xmlns:context='http://www.springframework.org/schema/context'
xsi:schemaLocation='http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd'>
<bean id='velocityEngine' class='org.springframework.ui.velocity.VelocityEngineFactoryBean'>
<property name='velocityProperties'><value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value></property></bean>
<bean id='dataSourceRemote' class='org.springframework.jdbc.datasource.DriverManagerDataSource'
p:driverClassName='com.mysql.jdbc.Driver' p:url='jdbc:mysql://172.23.23.148/SIWP' p:username='webpil' p:password='s2i0w1p0pilandina'/>
<bean id='entity' class='org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean'>
<property name='dataSource' ref='dataSource'/>
<property name="jpaProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
</props>
</property>
</bean>
<bean id='transaction' class='org.springframework.orm.jpa.JpaTransactionManager' p:entityManagerFactory-ref='entity'/>
<bean id='appContextAware' class='com.pil.sile.core.view.util.AppContextAware'/>
<tx:annotation-driven transaction-manager='transaction'/>
<jee:jndi-lookup id='dataSource' jndi-name='jdbc/sile'/>
<context:annotation-config/>
</beans>
也许你可以告诉我如何定义那个 bean 类型,或者我做错了什么。欢迎任何帮助
我假设您正在使用 Tomcat 服务器。 在您的 applicationContext.xml 中,它正在尝试查找容器提供的 JNDI 数据源。
您需要在 tomcat 的 server.xml 中声明 JNDI 资源,如下所示。 提供数据库相关配置,如url、用户名、密码、驱动程序class等
<GlobalNamingResources>
<Resource name="jdbc/sile"
auth="Container"
type="javax.sql.DataSource"
username="dbUser"
password="dbPassword"
url="jdbc:postgresql://localhost/dbname"
driverClassName="org.postgresql.Driver"
initialSize="20"
maxWaitMillis="15000"
maxTotal="75"
maxIdle="20"
maxAge="7200000"
testOnBorrow="true"
validationQuery="select 1" />
</GlobalNamingResources>
下一步是从 Tomcat 的 Web context.xml
引用创建的 JNDI 资源 <ResourceLink name="jdbc/DatabaseName"
global="jdbc/sile"
type="javax.sql.DataSource"/>
参考文档: