没有定义名为dataSource的bean

时间:2015-09-29 13:02:44

标签: spring spring-mvc spring-security

我正在使用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。

2 个答案:

答案 0 :(得分:1)

为了使这个配置正常工作,只需将其添加到Spring XML配置中:

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

这一行告诉Spring从包中加载@Configuration带注释的类&#34; com.your.package&#34; (假设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 -->

答案 1 :(得分:0)

另一个答案指出确保你有:

> 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() {