title: Java Generics Interface and Spring GetBean by Id and Class type
body: <p>I have a generic interface to which I can provide many implementation, For each such implementation I am associating a id attribute in the spring definition file.</p>
  
  <pre><code>IConfigurationService&lt;T&gt;
  </code></pre>
  
  <p>Now I am trying to write a utility method which can fetch me implementation by id.</p>
  
  <p>Following method is a utility method which throws me a warning message:</p>
  
  <pre><code>public &lt;T extends Serializable&gt; IConfigurationService&lt;T&gt; getBean(Class&lt;T&gt; type, String id){
          return (IConfigurationService&lt;T&gt;)context.getBean(id, IConfigurationService.class);
      }
  </code></pre>
  
  <p>Warning message:
  Type safety: Unchecked cast from IConfigurationService to IConfigurationService.</p>
  
  <p>How do I get rid of the this warning message?</p>
  
  <p>If I change the return statement as follows, then I am getting compilation error:</p>
  
  <pre><code>return (IConfigurationService&lt;T&gt;)context.getBean(id, IConfigurationService&lt;T&gt;.class);
  </code></pre>
  
  <p>I would be helpful if I someone can identify what am I doing wrong here.</p>