[java] Type safety: Unchecked cast

What did I do wrong? How do I resolve the issue?

Here :

Map<String,String> someMap = (Map<String,String>)getApplicationContext().getBean("someMap");

You use a legacy method that we generally don't want to use since that returns Object:

Object getBean(String name) throws BeansException;

The method to favor to get (for singleton) / create (for prototype) a bean from a bean factory is :

<T> T getBean(String name, Class<T> requiredType) throws BeansException;

Using it such as :

Map<String,String> someMap = app.getBean(Map.class,"someMap");

will compile but still with a unchecked conversion warning since all Map objects are not necessarily Map<String, String> objects.

But <T> T getBean(String name, Class<T> requiredType) throws BeansException; is not enough in bean generic classes such as generic collections since that requires to specify more than one class as parameter : the collection type and its generic type(s).

In this kind of scenario and in general, a better approach is not to use directly BeanFactory methods but let the framework to inject the bean.

The bean declaration :

@Configuration
public class MyConfiguration{

    @Bean
    public Map<String, String> someMap() {
        Map<String, String> someMap = new HashMap();
        someMap.put("some_key", "some value");
        someMap.put("some_key_2", "some value");
        return someMap;
    }
}

The bean injection :

@Autowired
@Qualifier("someMap")
Map<String, String> someMap;

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to type-safety

What is the difference between a strongly typed language and a statically typed language? Type safety: Unchecked cast What is Type-safe? Generic type conversion FROM string

Examples related to unchecked

How do I compile with -Xlint:unchecked? if checkbox is checked, do this Catch checked change event of a checkbox What is SuppressWarnings ("unchecked") in Java? Type safety: Unchecked cast