Home > database >  Field XXX in XXXXX required a bean of type 'java.util.Map' that could not be found. Spring
Field XXX in XXXXX required a bean of type 'java.util.Map' that could not be found. Spring

Time:05-10

in my project, I have a login manager class - that calls to "tokenManager" class. the tokenManager class included a map of tokens, and save there the generated tokens.

I get this error message:

APPLICATION FAILED TO START
***************************

Description:

Field tokens in com.chana.login.TokenManager required a bean of type 'java.util.Map' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'java.util.Map' in your configuration.

The tokenManager class defines a bean of type "map" (with @Autowired) so I don't know how to resolve that. Does anyone have an idea?

tokenManager.class:

@Service
@AllArgsConstructor
public class TokenManager {
    @Autowired
    private Map<String, TokenInfo> tokens;
    
    public boolean isTokenExists(String token) {
        return tokens.get(token) != null;
    }
    
    public String generageToken(ClientType type) {
        TokenInfo info = TokenInfo.generate(type);
        tokens.put(info.getToken(), info);
        return info.getToken();
                                        
    }
    public void removeToken(String token) {
        tokens.remove(token);
    }
    private boolean isTokenExpired(Date time) {
        return new Date().after(DateUtils.addMinutes(time, 30));
    }
    public void removeExpired() {
        tokens.entrySet().removeIf((entry)-> 
                isTokenExpired(entry.getValue().getCreationDate()));
    }
}

The loginManager.class call to tokenManager.class:

@Component
public class LoginManager {
    private final ApplicationContext context;
    @Autowired
    private final AdminService adminService;
    @Autowired
    private TokenManager tokenManager ;
    @Autowired
    private ClientService clientService;
    
    @Autowired

    public LoginManager(ApplicationContext context, AdminService adminService, TokenManager tokenManager) {
        this.context = context;
        this.adminService = adminService;
        this.tokenManager = tokenManager;
    }

//continue...

CodePudding user response:

No need of autowire in the TokenManager

    @Service
    @AllArgsConstructor
    public class TokenManager {
        private Map<String, TokenInfo> tokens = new HashMap<>();
...

This should be enough

  • Related