Feber Castellon
Feber Castellon

Reputation: 579

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

I have just copied a project and when I compile I have an error in my applicationContent.xml, when I put the mouse over the red='dataSource' within the bean 'entity' I got the error "Bean must be of 'javax.sql.DataSource' type, exception " and I am trying to find how to make it work, but I couldn't.

The log error says:

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

And of course this my applicationContext.xml file:

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

maybe you can tell me how to define that bean type, or maybe I am doing something else wrong. Any help is welcome

Upvotes: 1

Views: 2375

Answers (1)

Raj_89
Raj_89

Reputation: 721

I am assuming you are using Tomcat server. In your applicationContext.xml, it is trying to lookup a JNDI DataSource provided by the container.

You need to declare the JNDI resource in tomcat's server.xml something like below. Provide db related configuration like url, username, password, driver class etc.

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

Next step is to reference the created JNDI resource from Tomcat's web context.xml

 <ResourceLink name="jdbc/DatabaseName"
   global="jdbc/sile"
   type="javax.sql.DataSource"/>

Reference documentation:

Upvotes: 1

Related Questions