需求描述
购买手机分为三个手机厂商:华为手机、小米手机、OPPO手机,根据每个手机厂商编码查看对应厂商旗舰手机。
代码实现
初始化容器配置
| import lombok.extern.slf4j.Slf4j; |
| import org.springframework.beans.BeansException; |
| import org.springframework.context.ApplicationContext; |
| import org.springframework.context.ApplicationContextAware; |
| import org.springframework.stereotype.Component; |
| |
| |
| |
| |
| |
| |
| |
| @Component |
| @Slf4j |
| public class AppContext implements ApplicationContextAware { |
| |
| private static ApplicationContext appContext; |
| |
| @Override |
| public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| if (null == appContext) { |
| if (null != applicationContext) { |
| AppContext.appContext = applicationContext; |
| } |
| log.info("Spring init successfully"); |
| } |
| |
| } |
| |
| public static ApplicationContext getContext() { |
| if (appContext == null) { |
| throw new IllegalStateException("applicationContext inject failure,please restart the service"); |
| } |
| return appContext; |
| } |
| } |
厂商类型枚举
| |
| |
| |
| |
| |
| |
| public enum SourceTypeEnum { |
| |
| HUAWEI(1, "华为厂商"), |
| XIAOMI(2, "小米厂商"), |
| OPPO(3, "OPPO厂商"); |
| |
| private Integer type; |
| |
| private String memo; |
| |
| SourceTypeEnum(Integer type, String memo) { |
| this.type = type; |
| this.memo = memo; |
| } |
| |
| public Integer getType() { |
| return type; |
| } |
| |
| public String getMemo() { |
| return memo; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static SourceTypeEnum getInstance(Integer type) { |
| if (null == type) { |
| return null; |
| } |
| for (SourceTypeEnum typeEnum : SourceTypeEnum.values()) { |
| if (typeEnum.type.equals(type)) { |
| return typeEnum; |
| } |
| } |
| return null; |
| } |
| } |
策略来源注解
| import com.example.demo.enums.SourceTypeEnum; |
| import org.springframework.stereotype.Component; |
| |
| import java.lang.annotation.Documented; |
| import java.lang.annotation.ElementType; |
| import java.lang.annotation.Inherited; |
| import java.lang.annotation.Retention; |
| import java.lang.annotation.RetentionPolicy; |
| import java.lang.annotation.Target; |
| |
| |
| |
| |
| |
| |
| |
| @Inherited |
| @Target({ElementType.TYPE, ElementType.METHOD}) |
| @Retention(RetentionPolicy.RUNTIME) |
| @Documented |
| @Component |
| public @interface SourceType { |
| |
| SourceTypeEnum value(); |
| |
| } |
厂商来源工厂配置
| |
| |
| |
| |
| public interface InitProcess { |
| |
| |
| |
| void init(); |
| } |
| import cn.hutool.core.map.MapUtil; |
| import com.example.demo.annotation.SourceType; |
| import com.example.demo.enums.SourceTypeEnum; |
| import com.example.demo.service.PhoneManufacturerService; |
| import lombok.extern.slf4j.Slf4j; |
| import org.springframework.stereotype.Component; |
| |
| import java.util.Map; |
| import java.util.concurrent.ConcurrentHashMap; |
| |
| |
| |
| |
| |
| @Component |
| @Slf4j |
| public class FactoryHandlerStrategy implements InitProcess { |
| |
| private static ConcurrentHashMap<SourceTypeEnum, PhoneManufacturerService> phoneManufacturerMap = new ConcurrentHashMap<>(); |
| |
| @Override |
| public void init() { |
| initPhoneManufacturer(); |
| } |
| |
| |
| |
| |
| private void initPhoneManufacturer() { |
| |
| Map<String, PhoneManufacturerService> mapPhoneManufacturer = AppContext.getContext().getBeansOfType(PhoneManufacturerService.class); |
| mapPhoneManufacturer.entrySet().stream().forEach(entry -> { |
| Class<? extends Object> clazz = entry.getValue().getClass(); |
| SourceType annotation = clazz.getAnnotation(SourceType.class); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (null == annotation) { |
| return; |
| } |
| SourceTypeEnum sourceType = annotation.value(); |
| if (null != sourceType) { |
| PhoneManufacturerService phoneManufacturerService = phoneManufacturerMap.get(sourceType); |
| if (null != phoneManufacturerService) { |
| phoneManufacturerMap.remove(sourceType); |
| } |
| phoneManufacturerMap.put(sourceType, entry.getValue()); |
| } |
| }); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public PhoneManufacturerService getPhoneManufacturer(SourceTypeEnum sourceType) { |
| if (MapUtil.isEmpty(phoneManufacturerMap)) { |
| initPhoneManufacturer(); |
| } |
| return phoneManufacturerMap.get(sourceType); |
| } |
| |
| |
| } |
初始化来源工厂配置
| import org.springframework.boot.ApplicationArguments; |
| import org.springframework.boot.ApplicationRunner; |
| import org.springframework.stereotype.Component; |
| |
| |
| |
| |
| |
| |
| |
| @Component |
| public class InitContext implements ApplicationRunner { |
| |
| private final FactoryHandlerStrategy factoryHandlerStrategy; |
| |
| public InitContext(FactoryHandlerStrategy factoryHandlerStrategy) { |
| this.factoryHandlerStrategy = factoryHandlerStrategy; |
| } |
| |
| @Override |
| public void run(ApplicationArguments applicationArguments) throws Exception { |
| |
| |
| |
| |
| factoryHandlerStrategy.init(); |
| } |
| } |
业务代码
| |
| |
| |
| |
| public interface PhoneManufacturerService { |
| |
| |
| |
| |
| |
| |
| |
| String getPhone(Integer type); |
| |
| |
| |
| |
| |
| |
| |
| String buyPhone(Integer type); |
| |
| } |
| import com.example.demo.annotation.SourceType; |
| import com.example.demo.enums.SourceTypeEnum; |
| import com.example.demo.service.PhoneManufacturerService; |
| |
| |
| |
| |
| |
| |
| |
| @SourceType(SourceTypeEnum.OPPO) |
| public class OppoManufacturerServiceImpl implements PhoneManufacturerService { |
| @Override |
| public String getPhone(Integer type) { |
| return "一台OPPOFindX500旗舰手机"; |
| } |
| |
| @Override |
| public String buyPhone(Integer type) { |
| return "购买了OPPOFindX500旗舰手机"; |
| } |
| } |
| import com.example.demo.annotation.SourceType; |
| import com.example.demo.enums.SourceTypeEnum; |
| import com.example.demo.service.PhoneManufacturerService; |
| |
| |
| |
| |
| |
| |
| |
| @SourceType(SourceTypeEnum.XIAOMI) |
| public class XiaomiManufacturerServiceImpl implements PhoneManufacturerService { |
| @Override |
| public String getPhone(Integer type) { |
| return "一台小米100pro旗舰手机"; |
| } |
| |
| @Override |
| public String buyPhone(Integer type) { |
| return "购买了小米100pro旗舰手机"; |
| } |
| } |
| import com.example.demo.annotation.SourceType; |
| import com.example.demo.enums.SourceTypeEnum; |
| import com.example.demo.service.PhoneManufacturerService; |
| |
| |
| |
| |
| |
| |
| |
| @SourceType(SourceTypeEnum.HUAWEI) |
| public class HuaweiManufacturerServiceImpl implements PhoneManufacturerService { |
| |
| @Override |
| public String getPhone(Integer type) { |
| return "一台华为Mete100旗舰手机"; |
| } |
| |
| @Override |
| public String buyPhone(Integer type) { |
| return "购买了华为Mete100旗舰手机"; |
| } |
| } |
| import com.example.demo.config.FactoryHandlerStrategy; |
| import com.example.demo.enums.SourceTypeEnum; |
| import com.example.demo.service.PhoneManufacturerService; |
| import org.springframework.web.bind.annotation.GetMapping; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RequestParam; |
| import org.springframework.web.bind.annotation.RestController; |
| |
| |
| |
| |
| |
| @RestController |
| @RequestMapping("/phone") |
| public class PhoneController { |
| |
| private final FactoryHandlerStrategy factoryHandlerStrategy; |
| |
| public PhoneController(FactoryHandlerStrategy factoryHandlerStrategy) { |
| this.factoryHandlerStrategy = factoryHandlerStrategy; |
| } |
| |
| |
| @GetMapping("/get") |
| public String getPhone(@RequestParam Integer type) { |
| |
| PhoneManufacturerService manufacturerService = factoryHandlerStrategy.getPhoneManufacturer(SourceTypeEnum.getInstance(type)); |
| if (null != manufacturerService) { |
| return manufacturerService.getPhone(type); |
| } |
| return "获取失败了"; |
| } |
| |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步