Error while autowiring Spring repository

1.5k Views Asked by At

I have a repository for the entity 'AppUser' as follows:

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

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

It is autowired into my UserService class as:

@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);
}

The UserService is autowired into my CustomUserDetailsService as:

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

which is finally autowired to the WebSecurityConfiguration as:

@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
    }

UPDATE: I followed some of the suggestions here and have updated the error and the WebSecurityConfiguration file as shown above.

The error I now get is:

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)}

So, I assume I need to do somehow two things:

  1. Provide it a qualifying bean of type DataSource (I was thinking that the repo should qualify but it somehow isn't)

  2. Do the setFilterChainProxySecurityConfigurer. Not sure how to go about this. I followed a tutorial and created:

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

    To add SpringSecurityFilterChain, I assume I need to call addFilter here but it needs a filter class. Do I need to create that? Also, is this initializer automatically picked up by the dispatcher servlet or do I need to configure that somehow?

1

There are 1 best solutions below

10
On

You just need to wire your DataSource in WebSecurityConfiguration like this:

@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);
    }
}

And not sure if you are alreading doing it, but remember to add the SpringSecurityFilterChaint to your context.