Polos
Polos

Reputation: 85

Cant autowire an interface

I was following Spring Security tutorial at youtube and got stuck with autowiring simple interface at one of the services.

Service:

@Service
public class ApplicationUserService implements UserDetailsService {

    private final ApplicationUserDAO applicationUserDAO;

    @Autowired
    public ApplicationUserService(ApplicationUserDAO applicationUserDAO) {
        this.applicationUserDAO = applicationUserDAO;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return applicationUserDAO.selectApplicationUserByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException(String.format("Username %s not found.", username)));
    }
}

ApplicationUserDAO:

import java.util.Optional;


public interface ApplicationUserDAO {

    Optional<ApplicationUser> selectApplicationUserByUsername(String userName);
}

Service constructor parameter applicationUserDAO is highlighted by Idea with error "Could not autowire. No beans of 'ApplicationUserDAO' type found. "

I tried to annotate DAO with @Component and Idea calmed down, but i still got "Parameter 0 of constructor in com.example.demo.auth.ApplicationUserService required a bean of type 'com.example.demo.auth.ApplicationUserDAO' that could not be found." when i tried to launch the application.

Upvotes: 0

Views: 449

Answers (1)

Semih Ozturk
Semih Ozturk

Reputation: 57

You should create a bean that implemented by ApplicationUserDAO.

@Component
public class ApplicationUserDAOImpl implements ApplicationUserDAO {
    @Override 
    Optional<ApplicationUser> selectApplicationUserByUsername(String userName) {
         return null;
    }
}

Upvotes: 1

Related Questions