Java 使用spring注释将值注入地图
我用的是弹簧。我将主要注入组件和服务。但现在我想用enum键和“Cache”实现的注入值初始化一个映射,这样给定enum,我就可以让对象刷新缓存Java 使用spring注释将值注入地图,java,spring,spring-mvc,Java,Spring,Spring Mvc,我用的是弹簧。我将主要注入组件和服务。但现在我想用enum键和“Cache”实现的注入值初始化一个映射,这样给定enum,我就可以让对象刷新缓存 Map<String,Cache> Key Value "category" @Inject Category "attr" @Inject Attr "country" @Inject Country 它可以通过XML(如bellow或at)完成,但我想通过注释来完成 <
Map<String,Cache>
Key Value
"category" @Inject Category
"attr" @Inject Attr
"country" @Inject Country
它可以通过XML(如bellow或at)完成,但我想通过注释来完成
<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表达式语言完成此操作 我已转换了以下地图定义:
<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 { }
注意,不需要显式地将作用域设置为singleton,因为这是Spring中的默认值。此外,不需要使用@限定符
;通过@Component(“beanName”)
设置bean名称就足够了
将单例bean实例注入映射的最简单方法如下:
@Autowired
Map<String, Cache> map;
@Autowired
地图;
这将有效地将
缓存的所有子类自动关联到映射,键是bean名称。我不确定(没有以这种方式使用),但是你试过@Bean
方法吗?没有,我没有用过它,因为在中,我无法映射如何使用它。我可以用它来解决上面的问题吗?就像我为解决你的问题而探索的一样,这很有效!你能告诉我如何注入这些值吗。而不是“新的”,因为bean需要是单态的。如果您需要将其设置为单例,您可以在参考手册中查找静态方法调用。如果您需要使用Spring注入,请将其定义为bean并使用@
定义。@AdrianMenendez,您与Mkyong或我有问题。请阅读问题,看看我写了什么。您可能是从其他站点复制的人。@请确保您正在扫描缓存子类,并且映射是另一个也正在扫描的Springbean的字段。
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());
}
}
@Component("category")
class Category extends Cache { }
@Component("attr")
class Attr extends Cache { }
@Component("country")
class Country extends Cache { }
@Autowired
Map<String, Cache> map;