使用XML

时间:2016-05-02 13:28:16

标签: java spring

所以这是我的问题 -

假设我有几项服务,Service_A,Service_B等。(所有扩展服务类)

每个服务都是一个bean,并且有一个函数getKey(),它返回服务的密钥 - 类型为ServiceKey。

现在,我想定义一个新的bean - ServiceManager。这将有一个memeber:

HashMap<ServiceKey,Service> serviceMap.

我想问一下,是否有办法使用XML中的bean定义将所有现有的Service类型bean注入此Map memeber?

这样的事情对我没有帮助:

<bean id="serviceManager" class="ServiceManager">
    <property name="serviceMap">
        <map>
            <entry key="A" value="service_a"/>
            <entry key="B" value="service_a"/>
            <entry key="C" value="service_c"/>
        </map>
    </property>
</bean>

我不希望它硬编码,我想知道是否有办法获取每个现有的服务类,并将其定义为使用每个服务的getKey()函数来映射地图

信息getKey(),服务

提前感谢!

1 个答案:

答案 0 :(得分:0)

当将特定类型的所有bean注入Map时,目前无法指定自定义键。 Docs说:

  

只要预期的密钥类型是,即使是类型化的地图也可以自动装配   串。 Map值将包含预期类型的​​所有bean,   并且键将包含相应的bean名称

因此,解决方案是在Collection<Service> services中使用ServiceManager并自动装配所有Service个bean。然后有一个afterPropertiesSet(),它会将集合转换为Map。

仅使用xml的示例:

context.xml中

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="serviceManager" class="com.test.ServiceManager" autowire="byType" />

    <bean class="com.test.Service1"></bean>
    <bean class="com.test.Service2"></bean>

</beans>

TestApp

package com.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        ServiceManager serviceManager = context.getBean(ServiceManager.class);
        System.out.println(serviceManager.getServiceMap());
    }
}

的ServiceManager

package com.test;

import java.util.Collection;
import java.util.HashMap;

import org.springframework.beans.factory.InitializingBean;

public class ServiceManager implements InitializingBean {

    private Collection<Service> services;

    HashMap<ServiceKey, Service> serviceMap = new HashMap<ServiceKey, Service>();

    public Collection<Service> getServices() {
        return services;
    }

    public void setServices(Collection<Service> services) {
        this.services = services;
    }

    public HashMap<ServiceKey, Service> getServiceMap() {
        return serviceMap;
    }

    public void afterPropertiesSet() throws Exception {
        for (Service service : services) {
            serviceMap.put(service.getKey(), service);
        }
    }
}

服务

package com.test;

public interface Service {

    ServiceKey getKey();
}

服务关键字

package com.test;

public class ServiceKey {

    private String key;

    public ServiceKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String toString() {
        return key;
    }

}

服务1

package com.test;

public class Service1 implements Service {

    public ServiceKey getKey() {
        return new ServiceKey("one");
    }

}

服务2

package com.test;

public class Service2 implements Service {

    public ServiceKey getKey() {
        return new ServiceKey("two");
    }
}