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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12611422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:32:59  来源:igfitidea点击:

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

javajspstruts2spring-security

提问by rafuru

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/

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

And i have created a datasource.xmlfile with this content:

我创建了一个包含以下内容的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>

and this is my security.xml:

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

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

I don't have idea what is wrong :(

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

回答by Alex

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

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

回答by vdolez

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

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

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

缺少一个 jar,因为它没有部署。在启动服务器之前简单地将它添加到 tomcat 就可以发挥作用。