In this article we can see an of usage of Spring caching where the key of a cache entry is a concatenation of values and where a bean reference us used.

Configuration class with bean declaration for CacheManager and referecend bean passwordEncoder.

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class MyConfig {
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("mycache");

	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();

A spring bean where method authenticate is annotated with @Cacheable. With this key definition the key of a cache entry would be passed username concatenated with bcrypt hashing of the password.

import org.springframework.cache.annotation.Cacheable;

public class MyBean {

	@Cacheable(value="mycache", key="#username + @passwordEncoder.encode(#password)")
	public void authenticate(String username, String password) {
            // ...