无法解析对bean'DataSource'Spring3 + Struts 2的引用

时间:2012-09-26 22:28:05

标签: java jsp struts2 spring-security

我正在尝试使用数据库从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时无法解析对bean'DataSource'的引用   property'dataSource';嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   名为'dataSource'的bean已定义

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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

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

一个罐子丢失了,因为它没有部署。只需在启动服务器之前将其添加到tomcat就可以实现神奇。