Well, first of all, you're wasting memory with the new HashMap creation call. Your second line completely disregards the reference to this created hashmap, making it then available to the garbage collector. So, don't do that, use: private Map < String , String > someMap = ( HashMap < String , String >) getApplicationContext (). getBean ( "someMap" ); Secondly, the compiler is complaining that you cast the object to a HashMap without checking if it is a HashMap . But, even if you were to do: if ( getApplicationContext (). getBean ( "someMap" ) instanceof HashMap ) { private Map < String , String > someMap = ( HashMap < String , String >) getApplicationContext (). getBean ( "someMap" ); } You would probably still get this warning. The problem is, getBean returns Object , so it is unknown what the type is. Converting it to HashMap directly would not cause the problem with the sec...
Experienced Sofware Engineer