使用spring注释将值注入到地图中

时间:2015-09-22 11:01:37

标签: java spring spring-mvc

我正在使用春天。大多数情况下,我会注入组件和服务。但是现在我想用枚举键初始化一个映射并注入“Cache”实现的值,这样给定枚举我可以让对象刷新缓存。

Map<String,Cache>

Key           Value
"category"    @Inject Category     
"attr"        @Inject Attr
"country"     @Inject Country

我的班级就像

public abstract class Cache{
    refreshCache() {
      clearCache();
      createCache();
    }
    clearCache();
    createCache();
}

@Component
@Scope("singleton")
@Qualifier("category")
class Category extends Cache{}

@Component
@Scope("singleton")
@Qualifier("attr")
class Attr extends Cache{}

@Component
@Scope("singleton")
@Qualifier("country")
class Country extends Cache{}

可以通过XML(如下文或link)来完成,但我想用注释来完成。

<property name="maps">
        <map>
            <entry key="Key 1" value="1" />
            <entry key="Key 2" value-ref="PersonBean" />
            <entry key="Key 3">
                <bean class="com.mkyong.common.Person">
                    <property name="name" value="mkyongMap" />
                    <property name="address" value="address" />
                    <property name="age" value="28" />
                </bean>
            </entry>
        </map>
    </property>

2 个答案:

答案 0 :(得分:5)

如果在Spring上下文中有以下bean:

@Component("category")
class Category extends Cache { }

@Component("attr")
class Attr extends Cache { }

@Component("country")
class Country extends Cache { }

请注意,不需要将范围显式设置为singleton,因为这是Spring中的默认值。此外,没有必要使用@Qualifier;它足以通过@Component("beanName")设置bean名称。

将单例bean实例注入地图的最简单方法如下:

@Autowired
Map<String, Cache> map;

这将有效地将Cache的所有子类自动装配到地图中,其中键是bean名称。

答案 1 :(得分:-1)

您可以使用Spring Expression Language完成此操作。

我已经转换了您的以下地图定义:

<property name="maps">
    <map>
        <entry key="Key 1" value="1" />
        <entry key="Key 2" value-ref="PersonBean" />
        <entry key="Key 3">
            <bean class="com.mkyong.common.Person">
                <property name="name" value="mkyongMap" />
                <property name="address" value="address" />
                <property name="age" value="28" />
            </bean>
        </entry>
    </map>
</property>

基于注释。以下是代码:

PersonBean

@Component("PersonBean")
public class PersonBean {
    public String getMessage() {
        return "hello!";
    }
}

package com.mkyong.common;

public class Person {
    private String name;
    private String address;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

PersonConfiguration

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mkyong.common.Person;

@Configuration
public class PersonConfiguration {
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("mkyongMap");
        person.setAddress("address");
        person.setAge(28);
        return person;
    }
}

的TestController

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mkyong.common.Person;

@Controller
public class TestController {

    @Value("#{{'Key 1':1, 'Key 2':@PersonBean, 'Key 3': @person}}")
    Map testMap;

    @RequestMapping("/test")
    public void testMethod() {
        System.out.println(testMap.get("Key 1"));
        PersonBean personBean = (PersonBean) testMap.get("Key 2");
        System.out.println(personBean.getMessage());
        Person person = (Person) testMap.get("Key 3");
        System.out.println("Name: " + person.getName() + ", Address: "
                + person.getAddress() + ", Age: " + person.getAge());
    }

}