自动装配Spring存储库时出错

时间:2014-12-24 08:46:56

标签: spring jpa autowired spring-android

我有一个实体的存储库' AppUser'如下:

@Repository
public interface AppUserRepository extends CrudRepository<AppUser, Long>{

    public AppUser findByUserName(String username);
    public void delete(AppUser user);
}

它自动连接到我的UserService类中:

@Service("userService")
public class UserService {

    @Autowired
    AppUserRepository repository;

public AppUser registerNewAppUser(AppUser user) {
    AppUser u = repository.findByUserName(user.getUsername());
    if (u!=null)
        return null;
    return repository.save(user);
}

UserService自动连接到我的CustomUserDetailsS​​ervice中:

@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    private UserService userService;

最终自动连接到WebSecurityConfiguration:

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource datasource;

@Autowired
private CustomUserDetailsService customUserDetailsService;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.jdbcAuthentication().dataSource(datasource)
                .and()
                .userDetailsService(customUserDetailsService);

        // @formatter:on
    }

更新:我在此处遵循了一些建议并更新了错误和WebSecurityConfiguration文件,如上所示。

我现在得到的错误是:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; 
nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private javax.sql.DataSource showcase.WebSecurityConfiguration.datasource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

所以,我认为我需要做两件事:

  1. 为它提供一个DataSource类型的合格bean(我当时认为repo应该符合条件,但它不知何故)

  2. 执行setFilterChainProxySecurityConfigurer。不知道该怎么做。我按照教程创建了:

    public class WebMVCApplicationInitializer implements WebApplicationInitializer {
    
    public void onStartup(ServletContext container) {
    ServletRegistration.Dynamic registration =
            container.addServlet("dispatcher", new DispatcherServlet());
    registration.setLoadOnStartup(1);
    registration.addMapping("*.request");
    }
    }
    

    要添加SpringSecurityFilterChain,我假设我需要在这里调用addFilter,但它需要一个过滤器类。我需要创建吗?此外,这个初始化程序是否由调度程序servlet自动获取,还是需要以某种方式进行配置?

1 个答案:

答案 0 :(得分:0)

您只需要在WebSecurityConfiguration中连接数据源,如下所示:

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private Datasource datasource;

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.dataSource(dataSource).userDetailsService(customUserDetailsService);
    }
}

并且不确定您是否正在进行此操作,但请记住将SpringSecurityFilterChaint添加到您的上下文中。