Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

util:map in Spring picking up every bean of the map type

Tags:

spring

map

I have a Spring map problem that is bringing me to tears.

My spring looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    ">

   <util:map id="mockMap">
        <entry key="userTest1" value="test1"/>
        <entry key="userTest2" value="test2"/>
        <entry key="userTest3" value="test6"/>
    </util:map>
</beans>

Then the code where I am autowiring this is as follows (irrelevant parts ommitted):

@Autowired
@Resource(name="mockMap")
Map<String, String> testMap;

@Test
public void testGetGearListActivityOK() {
    for (String key : testMap.keySet()) {
        System.out.println("key = " + key);
    }
}

Amazingly enough, this will actually give me an error on the autowiring step saying that there are no beans matching type String. However, if I change the map in the unit-test to be defined as Map then I get the follow output:

[junit] key = mockMap
[junit] key = org.springframework.context.annotation.internalConfigurationAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalAutowiredAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalRequiredAnnotationProcessor
[junit] key = org.springframework.context.annotation.internalCommonAnnotationProcessor
[junit] key = systemProperties
[junit] key = systemEnvironment
[junit] key = messageSource
[junit] key = applicationEventMulticaster
[junit] key = lifecycleProcessor

I have not yet been able to get the key section of the entry to actually show up as a key. If I change the map back to Map and add some into my spring then the map populates with those, using their id's as the keys.

I'm so confused here and have used spring a decent amount in the past. If anyone has a clue what the heck is going on here I'd be very grateful.

Thanks in advance.

Also, I saw this question: Auto-wiring a List using util schema gives NoSuchBeanDefinitionException

But the solution there is to use @Resource, which I am already doing..

like image 512
Craig Avatar asked Sep 07 '12 19:09

Craig


People also ask

How do you get all the beans in your Spring boot application?

In Spring Boot, you can use appContext. getBeanDefinitionNames() to get all the beans loaded by the Spring container.

How does Spring know which bean to use?

The identifier of the parameter matches the name of one of the beans from the context (which is the same as the name of the method annotated with @Bean that returns its value). In this case, Spring chooses that bean for which the name is the same as the parameter.


2 Answers

Remove @Autowired and only use the @Resource(name="mockMap")

like image 78
Ryan Avatar answered Oct 05 '22 22:10

Ryan


you can't autowire a collection like that.

However, you can try these:

@Resource(name="mockMap")
private Map<String, String> testMap;

or even:

@Value("#{mockMap}")
private Map<String, String> testMap;

Check the spring docs, the tips section:

beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans

like image 28
Zigri2612 Avatar answered Oct 05 '22 22:10

Zigri2612