繁体   English   中英

Bean 必须是 'javax.sql.DataSource' 类型,Java Spring 例外

[英]Bean must be of 'javax.sql.DataSource' type, exception with Java Spring

我刚刚复制了一个项目,编译时我的 applicationContent.xml 出现错误,当我将鼠标放在 bean 'entity' 中的 red='dataSource' 上时,出现错误“Bean must be of '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 资源,如下所示。 提供db相关配置如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"/>

参考文档:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM