1
I always run the method that accesses what is in another class with @Autowired it is null, follow the code below:
@Component
public class TokenAuthenticationService {
@Autowired
UserRoleServiceSecurity service;
...
void addAuthentication(HttpServletResponse response, String username) throws IOException {
UserRole us = service.buscarPorUsername(username); //Aqui fica nulo e solta um java.lang.NullPointerException: null
System.out.println(us.getRole());
The Userroleservicesecurity.java class:
@Service
public class UserRoleServiceSecurity {
@Autowired
UsuarioRepository repository;
@Autowired
UsuarioRoleRepository usRepository;
public UserRole buscarPorUsername(String username) {
return usRepository.buscarUserUsername(username);
}
}
Is Userroleservicesecurity invalid? What error is displayed? How are you using Tokenauthenticationservice?
– Dherik
What are the packages? How is the
ComponentScan
?– Jefferson Quesado
I have not configured the Componentscan, I use this Userroleservicesecurity in other places and it works, It is the Userroleservicesecurity that comes null, all are in the same package.
– emanuel cavalcante
What error appears? Usually when spring fails to initialize a bean it throws an error. The problem may also be in the repositories that the service depends on.
– Patrícia Espada
the error is: java.lang.Nullpointerexception: null at com.baima.security.TokenAuthenticationService.addAuthentication(Tokenauthenticationservice.java:36) ~[classes/:na] at com.baima.security.JWTLoginFilter.successfulAuthentication(Jwtloginfilter.java:47) ~[classes/:na] at org.springframework.security.web.Authentication.AbstractAuthenticationProcessingFilter.doFilter(Abstractauthenticationprocessingfilter.java:240) ~[spring-security-web-4.2.3.RELEASE. jar:4.2.3.RELEASE] at org.springframework ... Continue but exceed maximum size
– emanuel cavalcante
How and in which class you are injecting
TokenAuthenticationService
?– Dherik