Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring注释-注入对象的映射_Java_Spring - Fatal编程技术网

Java Spring注释-注入对象的映射

Java Spring注释-注入对象的映射,java,spring,Java,Spring,使用XML注释,我使用下面的配置注入了一个映射- <bean id = "customerfactory" class = "com.brightstar.CustomerFactory"> <property name = "getCustomerMap"> <map key-type = "java.lang.String" value-type = "com.brightstar.CustomerImpl">

使用XML注释,我使用下面的配置注入了一个映射-

   <bean id = "customerfactory" class = "com.brightstar.CustomerFactory">
        <property name = "getCustomerMap">
            <map key-type = "java.lang.String" value-type = "com.brightstar.CustomerImpl">
                <entry key = "DEFAULT" value-ref = "getDefaultImpl"></entry>
                <entry key = "PERSON" value-ref = "getPersonImpl"></entry>
                <entry key = "COMPANY" value-ref = "getCompanyImpl"></entry>
            </map>
        </property>
    </bean>

我已经创建了3个bean——DefaultImpl、PersonImpl和CompanyImpl。如何使用Spring注释将它们作为映射注入

编辑:目前,我已经执行了以下操作,但不确定这是否是推荐的方法

private Map<String, CustomerImpl> getCustomerMap ;
@Autowired
private GetDefaultImpl getDefaultImpl;
@Autowired
private GetPersonImpl getPersonImpl;
@Autowired
private GetCompanyImpl getCompanyImpl;

private static final String DEFAULT = "DEFAULT";
private static final String COM = "PERSON";
private static final String SOM = "COMPANY";


@PostConstruct
public void init(){
    getCustomerMap = new LinkedHashMap<String,CustomerImpl>();
    getCustomerMap.put(DEFAULT, getDefaultImpl);
    getCustomerMap.put(PERSON, getPersonImpl);
    getCustomerMap.put(COMPANY, getCompanyImpl);        
}
私有映射getCustomerMap;
@自动连线
私有GetDefaultImpl GetDefaultImpl;
@自动连线
私有GetPersonImpl GetPersonImpl;
@自动连线
私有GetCompanyImpl GetCompanyImpl;
私有静态最终字符串DEFAULT=“DEFAULT”;
私有静态最终字符串COM=“PERSON”;
私有静态最终字符串SOM=“COMPANY”;
@施工后
公共void init(){
getCustomerMap=newLinkedHashMap();
getCustomerMap.put(默认值,getDefaultImpl);
getCustomerMap.put(PERSON,getPersonImpl);
getCustomerMap.put(公司,getCompanyImpl);
}

1.注入一个包含对象的映射(使用Java配置

你可以这样做

@Configuration
public class MyConfiguration {
    @Autowired private WhiteColourHandler whiteColourHandler;

    @Bean public Map<ColourEnum, ColourHandler> colourHandlers() {
        Map<ColourEnum, ColourHandler> map = new EnumMap<>();
        map.put(WHITE, whiteColourHandler);
        //put more objects into this map here
        return map;
    }
}
在代码中

@Value("#{${propertyname}}")  
private Map<String,String> propertyname;

加上我的2美分。据我所知,您正在实现工厂模式,在运行时在实现之间切换。所以,它是代码,不是配置,理想的位置是代码本身,而不是属性文件。我会采用Sundaraj Govindasamy建议的第一种方法。我认为@postConstruct方法也没有任何问题。但我会选择前者作为清洁剂。

谢谢。在我的例子中,我必须注入属性文件中不存在的对象。谢谢,这很有帮助。你能不能也对我的方法发表评论?我会接受答案。我不确定,@Postconstruct是初始化bean或管理bean生命周期的最佳实践,因为它是JEE标准,但不用于注入依赖项。但是,是的,最佳实践不是规则。谢谢@sundaraj!非常感谢您的投入!
@Value("#{${propertyname}}")  
private Map<String,String> propertyname;
    2.Values must be quotes, else you will get SpelEvaluationException