Problem statement:

I want to be able to create multiple objects of generic type without having to create each instance by hand and wire it in via @Bean.

For example: In the code below, I am trying to create DefaultRedisClient and CacheReadService for 2 different class types: Person and Blog. That's just an example. In reality, there could be 10's of classes/objects I would like to read from cache and for each one of those I am duplicating the code with slight modifications.

To create this block of code, again and again with only the CacheKey and CacheValue type changing along with couple other things seems wrong.


@Configuration
public class RedisCacheReadServiceConfig  {

    @Bean
    public DefaultRedisClient< PersonCacheValue > personRedisClient(
            @Autowired @Lazy
            RedisTemplate< String, String > redisTemplate) {

        return new DefaultRedisClient<>(redisTemplate, PERSON.getName());
    }

    @Bean
    public CacheReadService< PersonCacheKey, PersonCacheValue > personCacheReadServiceRedisImpl(
            @Lazy @Autowired 
            DefaultRedisClient< PersonCacheValue > personRedisClient,
            @Lazy @Autowired 
            PersonCacheFallback personCacheFallback,
            @Lazy @Autowired 
            RedisClientFactory< PersonCacheValue > redisClientFactory) {

        return new DefaultCacheReadService<>(personRedisClient, PERSON, personCacheFallback);
    }

    @Bean
    public DefaultRedisClient< BlogCacheValue > blogRedisClient(
            @Autowired @Lazy 
            RedisTemplate< String, String > redisTemplate) {

        return new DefaultRedisClient<>(redisTemplate, BLOG.getName());
    }

    @Bean
    public CacheReadService< 
            BlogCacheKey,
            BlogCacheValue > blogCacheReadServiceRedisImpl(
            @Lazy @Autowired 
            DefaultRedisClient< BlogCacheValue > blogRedisClient,
            @Lazy @Autowired 
            BlogCacheFallback blogCacheFallback,
            @Lazy @Autowired 
            RedisClientFactory< BlogCacheValue > redisClientFactory) {

        return new DefaultCacheReadService<>(blogRedisClient, BLOG, blogCacheFallback);
    }
}


Solution:

Fact of the matter is that there's a better way. Since Spring 4.0, Spring will automatically consider generics as a form of @Qualifier. And because of that we can have a factory pattern that uses generic types as @Qualifier. Please consider the example below:

/**
 * A factory that generates / provides DefaultRedisClient < V >
 * 
 * @param  CacheValue type
 */
@Service
public class RedisClientFactory< V extends CacheValue > {

    private final RedisTemplate< String, String > redisTemplate;

    @Autowired
    public RedisClientFactory(@Lazy RedisTemplate< String, String > redisTemplate) {

        this.redisTemplate = redisTemplate;
    }

    public DefaultRedisClient< V > getRedisClient(final String domainName) {

        return new DefaultRedisClient<>(redisTemplate, domainName);
    }
}

With the RedisClientFactory above, our main config becomes simpler because instead of creating DefaultRedisClient beans for different CacheValue types, we have delegated that work to RedisClientFactory and now can simply autowire DefaultRedisClient<V> by specifying the generic class type V. See line number 7 and 19 below. Spring is injecting a generic RedisClientFactory of the given type. Once we have that we can simply call the getRedisClient method to create the RedisClient we want.

@Configuration
public class RedisCacheReadServiceConfig  {

    @Bean
    public CacheReadService< PersonCacheKey, PersonCacheValue > personCacheReadServiceRedisImpl(
            @Lazy @Autowired 
            PersonCacheFallback personCacheFallback,
            @Lazy @Autowired 
            RedisClientFactory< PersonCacheValue > redisClientFactory) {

        return new DefaultCacheReadService<>(redisClientFactory.getRedisClient(PERSON.getName()), BLOG, personCacheFallback);
    }

    @Bean
    public CacheReadService < BlogCacheKey, BlogCacheValue > blogCacheReadServiceRedisImpl(
            @Lazy @Autowired 
            BlogCacheFallback blogCacheFallback,
            @Lazy @Autowired 
            RedisClientFactory< BlogCacheValue > redisClientFactory) {

        return new DefaultCacheReadService<>(redisClientFactory.getRedisClient(BLOG.getName()), BLOG, blogCacheFallback);
    }
}


But, we can go one step further and create a factory class to create CacheReadService for different CacheKey and CacheValue. See below:

Line #:

9 : We are injecting generic RedisClientFactory<V>
19 : We use generic RedisClientFactory<V> to create a generic DefaultRedisClient<V>
20 : This is pretty straight forward creation of CacheReadService<K,V>


@Service
public class CacheReadServiceFactory< K extends CacheKey, V extends CacheValue > {

  private final RedisClientFactory redisClientFactory;
  private final CacheFallback cacheFallback;

  @Autowired
  public CacheReadServiceFactory(
    @Lazy RedisClientFactory< V > redisClientFactory,
    @Lazy final CacheFallback< K, V > cacheFallback) {

    this.redisClientFactory = redisClientFactory;
    this.cacheFallback = cacheFallback;
  }

  public CacheReadService< K, V > getCacheReadService(
    final CacheDomainType cacheDomainType) {

    final DefaultRedisClient< V > redisClient = redisClientFactory.getRedisClient(cacheDomainType.getName());
    return new DefaultCacheReadService<>(redisClient, cacheDomainType, cacheFallback);
  }
}

Now, because of the CacheReadServiceFactory (above) our configuration class (below) becomes super simple because of Spring and being able to use generics for autowiring (since Spring 4.x).

Final Solution:



/**
 * Final Config class
 */
@Configuration
public class RedisCacheReadServiceConfig {

  @Bean
  public personCacheReadService(
    @Lazy @Autowired 
    final CacheReadServiceFactory< PersonCacheKey, PersonCacheValue > cacheReadServiceFactory
  ) {

    return cacheReadServiceFactory.getCacheReadService(PERSON);
  }

  @Bean
  public blogCacheReadService(
    @Lazy @Autowired 
    final CacheReadServiceFactory< BlogCacheKey, BlogCacheValue > cacheReadServiceFactory
  ) {

    return cacheReadServiceFactory.getCacheReadService(BLOG);
  }
}

Question / comments welcome. Just be nice. ;-)

Link to the article published on DZone: https://dzone.com/articles/spring-generic-factory-1


References:

1: https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics