springboot 将实现同一接口的Service注入到Map当中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33212500/article/details/82986081

applicationContext-dubboserviceproxy.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"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

	<description>将实现同一接口的Service注入到Map当中</description>

	<!--common_route_conf真正的提供者-->
	<bean class="com.example.demo13.service.Servicebean.impl.TestService" name="testService">
		<!-- 拉取数据处理器 -->
		<property name="queryServiceMap">
			<map>
				<entry key="one" value-ref="queryServiceOne"/>
				<entry key="two" value-ref="queryServiceTwo"/>
			</map>
		</property>
	</bean>

	<!--<bean id="queryServiceOne" class="com.example.demo13.service.Servicebean.impl.QueryServiceOne"></bean>
	<bean id="queryServiceTwo" class="com.example.demo13.service.Servicebean.impl.QueryServiceTwo"></bean>-->

</beans>
package com.example.demo13.service.Servicebean;

/**
 * 测试接口
 */
public interface QueryService {

    public String say();
}
package com.example.demo13.service.Servicebean.impl;

import com.example.demo13.service.Servicebean.QueryService;
import org.springframework.stereotype.Service;

@Service("queryServiceOne")
public class QueryServiceOne implements QueryService {
    @Override
    public String say() {
        return "queryServiceOne";
    }
}
package com.example.demo13.service.Servicebean.impl;

import com.example.demo13.service.Servicebean.QueryService;
import org.springframework.stereotype.Service;

@Service("queryServiceTwo")
public class QueryServiceTwo implements QueryService {
    @Override
    public String say() {
        return "queryServiceTwo";
    }
}
package com.example.demo13.service.Servicebean.impl;

import com.example.demo13.service.Servicebean.QueryService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("testService")
public class TestService {

    /** queryPageServiceMap映射 */
    @Getter
    @Setter
    private Map<String, QueryService> queryServiceMap;

    /**
     * 测试响应的bean
     * @param key
     * @return
     */
    public String test(String key){
        QueryService queryService = queryServiceMap.get(key);
        return queryService.say();
    }
}
@ImportResource(locations = {"spring/applicationContext-dubboserviceproxy.xml"})

方法2:

package com.example.demo13.service.Servicebean.impl;

import com.example.demo13.service.Servicebean.QueryService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Service
public class LoadMapService implements InitializingBean, ApplicationContextAware {
    private static Map<String, QueryService> queryServiceMap = null;
    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LoadMapService.applicationContext = applicationContext;
    }
    public <T>  Map<String, T> getBean(Class<T> beanType) {
        checkApplicationContext();
        Map<String, T> beanMap = applicationContext.getBeansOfType(beanType);
        if (CollectionUtils.isEmpty(beanMap)){
            throw new RuntimeException("beanMap获取失败");
        }
        return beanMap;
    }
    private static void checkApplicationContext(){
        Assert.isTrue(applicationContext != null, "应用上下文不能为空");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, QueryService> bean = getBean(QueryService.class);
        queryServiceMap = bean;
    }

    public QueryService getQueryService(String key){
        QueryService queryService = queryServiceMap.get(key);
        return queryService;
    }
}

测试

package com.example;

import com.example.demo13.Demo13Application;
import com.example.demo13.service.Servicebean.QueryService;
import com.example.demo13.service.Servicebean.impl.LoadMapService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Created by zhangtengda on 2018/5/30.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Demo13Application.class)
//@ImportResource(locations = {"spring/applicationContext-dubboserviceproxy.xml"})
public class CheckTest {

    @Autowired
    private LoadMapService loadMapService;

    @Test
    public void testPerson() {

        QueryService queryService = loadMapService.getQueryService("test1");
        System.out.println(queryService.say());

        queryService = loadMapService.getQueryService("test2");
        System.out.println(queryService.say());

        queryService = loadMapService.getQueryService("queryServiceOne");
        System.out.println(queryService.say());

        queryService = loadMapService.getQueryService("queryServiceTwo");
        System.out.println(queryService.say());
    }


}

方法3:

package com.example.demo13.service.Servicebean.impl;

import com.example.demo13.service.Servicebean.QueryService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class LoadMapService implements InitializingBean {
    private static Map<String, QueryService> queryServiceMap = null;

    @Resource
    private ApplicationContextLoad applicationContextLoad;
    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, QueryService> bean = applicationContextLoad.getBean(QueryService.class);
        queryServiceMap = bean;
    }

    public QueryService getQueryService(String key){
        QueryService queryService = queryServiceMap.get(key);
        return queryService;
    }
}
package com.example.demo13.service.Servicebean.impl;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Component
public class ApplicationContextLoad implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextLoad.applicationContext = applicationContext;
    }
    public <T> Map<String, T> getBean(Class<T> beanType) {
        checkApplicationContext();
        Map<String, T> beanMap = applicationContext.getBeansOfType(beanType);
        if (CollectionUtils.isEmpty(beanMap)){
            throw new RuntimeException("beanMap获取失败");
        }
        return beanMap;
    }
    private static void checkApplicationContext(){
        Assert.isTrue(applicationContext != null, "应用上下文不能为空");
    }

}

方法四:

package com.example.demo13.service.Servicebean.impl;

import com.example.demo13.service.Servicebean.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoadHashMapService {

    private static Map<String, QueryService> queryServiceMap = new HashMap<>();

    //@Autowired
    //@Qualifier("queryServiceOne")
    @Resource(name = "queryServiceOne")
    private QueryService queryServiceOne;

    @Autowired
    @Qualifier("queryServiceTwo")
    private QueryService queryServiceTwo;

    /**
     * @PostContruct是spring框架的注解 spring容器初始化的时候执行该方法
     */
    @PostConstruct
    private void init(){
        queryServiceMap.put("queryServiceOne", queryServiceOne);
        queryServiceMap.put("queryServiceTwo", queryServiceTwo);
    }

    public void say(String key){
        String say = queryServiceMap.get(key).say();
        System.out.println(say);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33212500/article/details/82986081