Jose Ospina Jose Ospina - 25 days ago 24
Java Question

Spring boot @Autowired is null for a bean in selective cases

Spring boot @Autowire is

null
in a selective case without apparent reason:

So, I have a service class:

package org.collectiveone.services;

import java.util.List;

import org.collectiveone.domain.Role;
import org.collectiveone.repositories.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceIm implements RoleServiceIf {

@Autowired
private RoleRepository roleRepository;

...


Which I am using in three different places, within the same package, and it works fine in two out of three:

Works fine here:

package org.collectiveone.services;


import javax.annotation.PostConstruct;

import org.collectiveone.domain.Role;
import org.collectiveone.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StartUpMethods {

...

@Autowired
RoleServiceIf roleService;


@PostConstruct
public void UpdateDecisionsStatus() {

roleService.save(new Role());

...


Works fine here:

package org.collectiveone.services;

import org.collectiveone.domain.User;
import org.collectiveone.dto.UserNewDto;
import org.collectiveone.repositories.UserRepository;
import org.collectiveone.services.error.PasswordsNotEqualException;
import org.collectiveone.services.error.UserAlreadyExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userDetailsService")
@Transactional
public class UserServiceIm implements UserServiceIf, UserDetailsService {

@Autowired
RoleServiceIf roleService;

@Override
public User registerNewUserAccount(final UserNewDto accountDto) {
...

roleService.addRoleTo(user.getUsername(),"ROLE_USER");

...

}


But does not works here:

package org.collectiveone.services;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.collectiveone.domain.Role;
import org.collectiveone.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;


@Service
public class UserDetailsIm implements UserDetails {

/**
*
*/
private static final long serialVersionUID = 1L;

@Autowired
RoleServiceIf roleService;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
...

List<Role> roles = roleService.getRolesOf(user.getUsername());

...
}


Any ideas? As you can see the case in which it does not work is for a class that implements the Spring-Security
UserDetails
interface. I am working with spring-boot 1.4.1.RELEASE.

Answer

Don't you instantiate UserDetailsIm outside of Spring context (using new)? Autowired by default works only on beans managed by Spring - created by container.

Comments