簡體   English   中英

使用Spring注釋將值注入地圖

[英]Inject values into map using spring annotation

我正在用彈簧。 通常,我將注入組件和服務。 但是,現在我想用枚舉鍵和注入“ 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>

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

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

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

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

注意,不需要將范圍顯式設置為單例,因為這是Spring的默認設置。 此外,無需使用@Qualifier 通過@Component("beanName")設置bean名稱就足夠了。

將單例bean實例注入到映射的最簡單方法如下:

@Autowired
Map<String, Cache> map;

這將有效地將Cache所有子類自動連接到映射,鍵為Bean名稱。

您可以使用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>

基於注釋。 下面是代碼:

人豆

@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;
    }
}

人員配置

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;
    }
}

測試控制器

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());
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM