这里Spring管理的Bean,可以认为是一个个的Service,每个Service都是一个服务接口

自动注册Service的好处:

1、根据指定的name/id获取对应的Service,实现简单工厂

2、服务自动注册到Map中,集中管理

方案1:通过接口实现


1、声明接口:定义接口的方法,这里用到的泛型可以根据实际需要忽略

 /**
* @Description: 资金请求处理 接口
* -- 资金类操作的公共接口
* @author: lishh
* @Date: 2019/1/16 20:40
*/
public interface IFundReqHandleService<Req extends BaseRequest, Resp extends BaseResponse, DO extends CommonDO> {
//接口定义忽略...
}

2、接口实现

实现举例:注意:每个实现类都定义了一个final的值,作为它的id,以后拿着这个id获取这个服务

/**
* @Description: 入金
* @author: lishh
* @Date: 2019/1/4 15:55
*/
@Service
public class FundinServiceImpl implements IFundReqHandleService<FundinReq, FundinResp, Fundin> {
private static Logger logger = LoggerFactory.getLogger(FundinServiceImpl.class);
private final RequestTypeEnum fundReqType = RequestTypeEnum.FUND_IN; @Autowired
private FundinMapper fundinMapper; //...略....
}

3、定义获取服务的工厂类:这里的RequestTypeEnum定义为枚举是为了可读性更好,使用字符串也可以

 /**
* @Description: 资金操作服务 工厂
* @author: lishh
* @Date: 2019/1/4 17:39
*/
public interface IFundServiceFactory { /**
* 根据请求类型 获取对应的handler
*
* @param fundType
* @return
*/
IFundReqHandleService getService(RequestTypeEnum fundType);
}

4、实现工厂类,提供服务获取的实现:代码很简单,关键就是注入Map<String, IFundReqHandleService> fundFlowMap;

 /**
* @Description: 资金操作服务 工厂
* @author: lishh
* @Date: 2019/1/4 17:44
*/
@Component("fundServiceFactory")
public class FundServiceFactoryImpl implements IFundServiceFactory, InitializingBean {
private static Logger logger = LoggerFactory.getLogger(FundServiceFactoryImpl.class); /**
* 注入实现IReqHandleService接口的所有服务
* key: service name
* value:服务引用
*/
@Autowired
Map<String, IFundReqHandleService> fundFlowMap; /**
* 资金操作handler Map
* key:服务类型
* value:服务引用
*/
private Map<RequestTypeEnum, IFundReqHandleService> serviceMap = new ConcurrentHashMap<>(); /**
* 获取服务
*
* @param fundType
* @return
*/
@Override
public IFundReqHandleService getService(RequestTypeEnum fundType) {
IFundReqHandleService service = this.getServiceMap().get(fundType);
if (null == service) {
logger.error("invalid or unsupport fundType:{},current serviceMap:{}",
JSON.toJSONString(fundType),
JSON.toJSONString(this.getServiceMap()));
} return service;
} /**
* 服务自动注册...
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
for (Map.Entry<String, IFundReqHandleService> entry : fundFlowMap.entrySet()) {
RequestTypeEnum reqType = entry.getValue().getReqType();
this.getServiceMap().put(reqType, entry.getValue());
logger.info("regist service:{}-->{}.current map size:{}", entry.getKey(), reqType.getReqType(), this.getServiceMap().size());
}
} public Map<RequestTypeEnum, IFundReqHandleService> getServiceMap() {
return serviceMap;
} }

5、使用

很简单,注入工厂类,提供id给你获取服务

 /**
* 资金类请求处理服务工厂
*/
@Resource(name = "fundServiceFactory")
private IFundServiceFactory fundServiceFactory; //...略。。。。 //01、获取业务处理对象
IFundReqHandleService reqHandler = fundServiceFactory.getService(requestType);
if (null == reqHandler) {
throw new SysException(ErrorCodeEnum.ACCT_SERVICE_ERROR, "unsupport requestType");
}
//。。。。调用服务接口,略,...

总结:

1、spring注入的理解

2、InitializingBean接口的理解

方案2:通过注解annotation实现


注解

注解

Spring Bean自动注册的实现方案的更多相关文章

  1. spring bean自动注入

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  2. Spring boot 梳理 - Spring boot自动注册DispatcherServlet

    spring boot提供的DispatcherServlet的name就是“dispatcherServlet”. 源码 public ServletRegistrationBean dispatc ...

  3. spring——bean自动装配

    注意:自动装配功能和手动装配要是同时使用,那么自动装配就不起作用. beans.xml <?xml version="1.0" encoding="UTF-8&qu ...

  4. Spring之2:Spring Bean动态注册、删除

    IoC容器的初始化包括BeanDefinition的Resource定位.载入和注册这三个基本的过程. 一.Resource定位.BeanDefinition的资源定位有resourceLoader通 ...

  5. Spring Bean自动装配有哪些方式?

    Spring 容器能够自动装配 Bean .也就是说,可以通过检查 BeanFactory 的内容让 Spring 自动解析 Bean 的协作者. 自动装配的不同模式: no - 这是默认设置,表示没 ...

  6. Spring Bean自动检测

    1-自动检测bean 需要用到<context:component-scan> 注意:a) 需要include进来xmlns:context命名空间:base-package指的是我们要扫 ...

  7. Spring 学习笔记(2) Spring Bean

    一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...

  8. Spring 自动装配及自动注册的相关配置

    Spring支持好几种自动装配(Autowiring)的方式,以及自动扫描并注册Bean的配置(在beans.xml中配置). 下文我们进行一个小结. 1. <context: annotati ...

  9. Spring Boot 使用Java代码创建Bean并注册到Spring中

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/catoop/article/details/50558333 声明同一个类下的多个实例: packa ...

随机推荐

  1. JUC总览,来自汪文君整理

  2. spring实现可重置时间定时器

    此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. spring-framework-4.3.4.RELEASE 的 lib ...

  3. SZU1

    CodeForces 518A 字符串进位.. #include <iostream> #include <string> #include <cstring> # ...

  4. MySql中存储引擎MyISAM与InnoDB区别于选择

    InnoDB: 支持事务处理等 不加锁读取 支持外键 支持行锁 不支持FULLTEXT类型的索引 不保存表的具体行数,扫描表来计算有多少行 DELETE 表时,是一行一行的删除 InnoDB 把数据和 ...

  5. 比较全的css重设

    一.最简化的CSS Reset(重设) : * { padding:; margin:; } 这是最普遍最简单的CSS重设,将所有元素的padding和margin值都设为0,可以避免一些浏览器在理解 ...

  6. 11.6NOIP模拟赛解题报告

    心路历程 预计得分:\(100 + 100 + 100 = 300\) 实际得分:\(100 +100 +100 = 300\) 学OI两年终于AK了一次qwq(虽然题目炒鸡水..) 纪念一下这令人激 ...

  7. Python-约束和异常处理

    今天我们来说一说类的规范以及程序出现错误后我们要怎么进行处理 一.类的约束 首先,你要清楚,约束是对类的约束,比如,现在你是一个项目经理,然后呢,你给手下的人分活,张三你处理一下普通用户登录,李四你处 ...

  8. Python入门-函数进阶

    昨天我们简单的了解了函数的定义,调用,以及传参,其实还有一个更重要的传参:动态传参,让我们继续昨天没有说完的,以及今天我要分享的东西. 一.动态传参 之前我们说过了传参,如果我们需要给一个函数传参,而 ...

  9. springlog记录

    在servlet.xml加入 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-aut ...

  10. 我要为运维说一句,我们不是网管,好不!!Are you know?

    运维 运维,这里指互联网运维,通常属于技术部门,与研发.测试.系统管理同为互联网产品技术支撑的4大部门,这个划分在国内和国外以及大小公司间都会多少有一些不同. 一个互联网产品的生成一般经历的过程是:产 ...