简体   繁体   中英

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

您是否通过<import resource="datasource.xml" />将文件包含在数据<import resource="datasource.xml" />

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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