Rafael Carrillo
Rafael Carrillo

Reputation: 2883

Cannot resolve reference to bean 'dataSource' Spring3 + Struts 2

I'm trying to manage the login system from spring with a Database, I have created my database following this tutorial: http://www.mkyong.com/spring/maven-spring-jdbc-example/

And i have created a datasource.xml file with this content:

<beans xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/login"/>
        <property name="username" value="root"/>
        <property name="password" value="pass"/>
    </bean>

</beans>

and this is my security.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Configuracion de Muestra
  -
  -->

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/security 
                http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>

      <http auto-config='true'>

    <intercept-url pattern="/**" access="ROLE_USER" />
    <form-login default-target-url='/example/index.jsp'
            always-use-default-target='true' />
    </http>

  <authentication-manager>
       <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource"

           users-by-username-query="
              select nombre,password 
              from usuarios where username=?" 

           authorities-by-username-query="
              select u.nombre, ur.rol from usuarios u, usuarios_roles ur 
              where u.user_id = ur.user_id and u.username =?  " 

        />
       </authentication-provider>
    </authentication-manager>

</beans:beans>

But I'm having the next error on deploy:

Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

I don't have idea what is wrong :(

Upvotes: 2

Views: 11225

Answers (2)

vdolez
vdolez

Reputation: 1118

I had this very error at the beginning of the logs, except I also had an error at the bottom of the logs : java.lang.NoClassDefFoundError

A jar was missing, because it was not deployed. Simply adding it to tomcat before starting the server did the magic.

Upvotes: 0

Alex
Alex

Reputation: 11579

Did you include the file with datasource via <import resource="datasource.xml" />?

Upvotes: 4

Related Questions