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

Posted

技术标签:

【中文标题】无法解析对 bean \'dataSource\' Spring3 + Struts 2 的引用【英文标题】:Cannot resolve reference to bean 'dataSource' Spring3 + Struts 2无法解析对 bean 'dataSource' Spring3 + Struts 2 的引用 【发布时间】:2012-09-18 15:37:40 【问题描述】:

我正在尝试使用数据库从 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' 的引用 属性“数据源”;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 定义了名为“dataSource”的 bean

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

【问题讨论】:

【参考方案1】:

您是否通过&lt;import resource="datasource.xml" /&gt; 将文件包含在数据源中?

【讨论】:

-_- 什么... *sight 。那是问题!呵呵谢谢:)【参考方案2】:

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

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

【讨论】:

以上是关于无法解析对 bean 'dataSource' Spring3 + Struts 2 的引用的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot创建动态数据源:当前正在创建请求的bean:是否存在无法解析的循环引用?

idea中applicationContext-trans.xml中的Cannot resolve bean 'dataSource'...的问题解决

Spring SAML 实现无法解析对 bean 的引用

Spring Boot 2.1 中的 DataSource bean 覆盖

无法启动休眠会话工厂 bean

一个组件需要一个名为“dataSource”的 bean,但找不到该 bean