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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder

@Configuration
@EnableCaching
public class MyConfig {
	@Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("mycache");
    }

	@Bean("passwordEncoder")
	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;

@Component
public class MyBean {

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