【问题标题】:No bean named dataSource is defined没有定义名为 dataSource 的 bean
【发布时间】:2015-09-29 13:02:43
【问题描述】:

我正在使用 Spring Security 4,我想通过查询数据库进行身份验证。我创建了用户和权限表。我的数据源的 jdbc 连接 bean 是使用注释在 java 文件中定义的。我的 spring 安全配置在 xml 中。我已经读到正在使用相同的 contextLoader,因此加载所有 bean 应该没有问题。但是,如何将 java 文件中定义的 dataSource bean 导入到我的 spring 安全配置文件中?

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
@MapperScan("com.iz.acsadmin.dao")
public class DataConfig{
    @Bean(name = "dataSource" )
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://192.168.1.3:5432/Student");
        dataSource.setUsername("postgres");
        dataSource.setPassword("admin");
        return dataSource;
    }
...

XML Spring-Config 文件

<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.xsd">  
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/test.htm" access="hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')" />
        <intercept-url pattern="/welcome.htm" access="hasRole('USER') or hasRole('ADMIN') or hasRole('DBA')" />        
        <form-login />
    </http>

    <authentication-manager >
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource" 
                users-by-username-query=
                "SELECT USERNAME, PASSWORD, ENABLED FROM USERS WHERE USERNAME=?"
                authorities-by-username-query=
                "SELECT USERNAME, AUTHORITY FROM AUTHORITIES WHERE USERNAME=?"
                />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

xml文件中的dataSource bean在java文件中找不到主dataSource bean。

【问题讨论】:

  • 您能否发布您的代码以听取或向我们展示更多详细信息?
  • 对不起,我是新来的。我应该再放一些文件吗?

标签: spring spring-mvc spring-security


【解决方案1】:

为了让这个配置工作,只需将它添加到你的 Spring XML 配置中:

<context:component-scan base-package="com.your.package" />

这一行告诉 Spring 从包“com.your.package”加载你的 @Configuration 注释类(假设 DataConfig 类在这个包中)。

对了,别忘了添加 Spring 上下文 XSD 位置:

<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                             http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">  

<context:component-scan base-package="com.your.package" />
<!-- some other configuration -->

【讨论】:

    【解决方案2】:

    正如另一个答案指出的那样,请确保您拥有:

    > In order to get this configuration working, simply add this to your
    > Spring XML config :
    > 
    > <context:component-scan base-package="com.your.package" /> This line
    > tells Spring to load your @Configuration annotated classes from the
    > package "com.your.package" (assuming that the DataConfig class is in
    > this package).
    

    当您使用它时,请确保您拥有:

    <context:annotation-config /> 
    

    我是 Spring 新手,但想知道作为名称的 dataSource 是否是唯一的。

    我会尝试两件事,将您的数据源注释为主要数据源,如下所示:

    @Primary
    @Bean(name = "dataSource" )
        public DataSource getDataSource() {
    

    如果这不起作用,我会尝试不同的名称并在安全配置中指向它:

    @Bean(name = "myDataSource" )
            public DataSource getDataSource() {
    

    【讨论】:

    • 感谢您的回复。我认为@Primary只有在有多个数据源时才需要,以避免冲突。是的,我一开始曾尝试更改 bean 的名称,但它找不到带注释的名称。此外,annotation-config 标记从一开始就存在。
    猜你喜欢
    • 2012-09-07
    • 2016-06-09
    • 1970-01-01
    • 2012-03-25
    • 2012-05-29
    • 2011-12-08
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多