1
@Repository
public interface Userrepo extends JpaRepository<Users, String> {

}

I want the above interface to be autowired in the below class

@Component
public class Userauth {
    
    
    @Autowired
    Userrepo urepo;
    
    public boolean check(String name,String password) {
        Application a=new Application();
        
        Optional<Users> u=urepo.findById(name);
        
        if(!u.isEmpty()) {
            Users ud=u.get();
            if(ud.getPassword().equals(password))
                return true;
        }
            
        return false;
    }

}

but its giving an error "urepo" is null

in the log getting this.

Ignored because not a concrete top-level class: file [C:\Users\Documents\workspace-spring-tool-suite-4-4.9.0.RELEASE\1Sampleproject\target\classes\com\example\demo\repos\Userrepo.class]
  • 2
    It cannot be `null` unless you are instantiating this yourself. This basically is a duplicate of https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null. More details https://deinum.biz/2020-07-03-Autowired-Field-Null/ – M. Deinum Mar 12 '21 at 18:51
  • The log warning happens likely, because you added the `@Repository` annotation on a JPA repository interface, however that annotation is meant for classes, which should be detected as beans, not JPA repository interfaces. – dunni Mar 12 '21 at 18:55
  • Could you please show the whole class and interfaces? I mean, including the package and the imports. Also, did you check you have the correct components scanning? – Enmanuel Rodríguez Paz Mar 12 '21 at 19:07

0 Answers0