2

我正在尝试使用数据库从 spring 管理登录系统,我按照本教程创建了我的数据库:http ://www.mkyong.com/spring/maven-spring-jdbc-example/

我创建了一个包含以下内容的datasource.xml文件:

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

这是我的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>

但是我在部署时遇到了下一个错误:

设置 bean 属性“dataSource”时无法解析对 bean“dataSource”的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

我不知道出了什么问题:(

4

2 回答 2

4

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

于 2012-09-26T23:38:17.530 回答
0

我在日志的开头遇到了这个错误,除了我在日志的底部也有一个错误:java.lang.NoClassDefFoundError

缺少一个 jar,因为它没有部署。只需在启动服务器之前将其添加到 tomcat 就可以了。

于 2015-08-25T14:52:48.970 回答