当前位置: 首页 > wzjs >正文

毕节建设公司网站网站建设如何工作

毕节建设公司网站,网站建设如何工作,设置网站域名解析和网站主机绑定,网站策划书总结首先,定义接口或抽象父类。例如,定义一个Strategy接口,里面有execute方法。然后不同的策略类实现这个接口,比如ConcreteStrategyA、ConcreteStrategyB等,每个类用Component注解,可能指定Bean的名字&#xf…

首先,定义接口或抽象父类。例如,定义一个Strategy接口,里面有execute方法。然后不同的策略类实现这个接口,比如ConcreteStrategyA、ConcreteStrategyB等,每个类用@Component注解,可能指定Bean的名字,或者用默认的类名首字母小写作为Bean名称。

然后,需要将这些Bean自动注入到一个Map中。Spring支持将同一类型的Bean注入到Map中,其中键是Bean的名称,值是对应的实例。例如,在某个服务类中,使用@Autowired private Map<String, Strategy> strategies; 这样,Spring会自动将所有的Strategy实现类放入这个Map,键是Bean的名称。

接下来,当需要执行某个策略时,根据传入的key(即Bean的名称)从Map中获取对应的实例,调用其execute方法。例如,在服务类中有一个方法,接收策略名称作为参数,然后调用strategies.get(name).execute()。

在Spring框架中,使用策略模式并结合依赖注入可以通过以下步骤实现将子类实例存入Map,并通过指定名称执行对应方法:

1. 定义策略接口或抽象类

public interface PaymentStrategy {void processPayment(double amount);
}

2. 实现具体策略类(子类)

使用@Component并为每个策略指定唯一的Bean名称:

@Component("creditCard")
public class CreditCardPayment implements PaymentStrategy {@Overridepublic void processPayment(double amount) {System.out.println("使用信用卡支付:" + amount);}
}@Component("alipay")
public class AlipayPayment implements PaymentStrategy {@Overridepublic void processPayment(double amount) {System.out.println("使用支付宝支付:" + amount);}
}

3. 注入策略Map并执行

@Service
public class PaymentService {private final Map<String, PaymentStrategy> strategies;// 通过构造器注入,Key为Bean名称,Value为实例@Autowiredpublic PaymentService(Map<String, PaymentStrategy> strategyMap) {this.strategies = strategyMap;}public void executePayment(String method, double amount) {PaymentStrategy strategy = strategies.get(method);if (strategy == null) {throw new IllegalArgumentException("无此支付方式: " + method);}strategy.processPayment(amount);}
}

4. 使用示例

@RestController
public class PaymentController {@Autowiredprivate PaymentService paymentService;@PostMapping("/pay")public String pay(@RequestParam String method, @RequestParam double amount) {paymentService.executePayment(method, amount);return "支付成功";}
}

关键点说明

  • 自动注入Map:Spring会自动将相同类型(PaymentStrategy)的Bean注入到Map中,Key为Bean名称,Value为实例。
  • 策略扩展:新增支付方式只需添加实现类并用@Component("新名称")标记,无需修改其他代码。
  • 异常处理:当传入不存在的Key时,需处理策略未找到的情况(如抛出异常或返回默认策略)。

自定义Map键名(可选)

若需使用非Bean名称的Key,可通过自定义注解实现:

定义注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PaymentType {String value();
}

应用注解到策略类

@Component
@PaymentType("credit")
public class CreditCardPayment implements PaymentStrategy { ... }

手动构建策略Map

@Service
public class PaymentService {private final Map<String, PaymentStrategy> strategies = new HashMap<>();@Autowiredpublic PaymentService(List<PaymentStrategy> strategyList) {for (PaymentStrategy strategy : strategyList) {PaymentType annotation = strategy.getClass().getAnnotation(PaymentType.class);if (annotation != null) {strategies.put(annotation.value(), strategy);}}}// 其他代码不变
}

这种方法允许更灵活地定义策略名称,同时保持代码的可扩展性。

@sercice(“queryability”)

public class QueryAbilityService implement Strategy{…}

private Map<String, Strategy> strategy;

strategy.get(“key”).ifPresent(handler->handler.onmessage(input));


文章转载自:

http://0BzdhW6j.pjwmL.cn
http://RUSreeUV.pjwmL.cn
http://Fn25POpQ.pjwmL.cn
http://qal7ZxOW.pjwmL.cn
http://TeTG7WZD.pjwmL.cn
http://sZitCgMS.pjwmL.cn
http://4AW8IZlU.pjwmL.cn
http://m3k7JqWx.pjwmL.cn
http://I7QER2aT.pjwmL.cn
http://ONH2w5WN.pjwmL.cn
http://RBOWyNuu.pjwmL.cn
http://CXN3hZfN.pjwmL.cn
http://AWMMkmfA.pjwmL.cn
http://AMFU7Ty2.pjwmL.cn
http://qg5lpW3Z.pjwmL.cn
http://worANPqq.pjwmL.cn
http://0OzPeoGn.pjwmL.cn
http://hiAJVg7a.pjwmL.cn
http://ACSuKNrH.pjwmL.cn
http://cJBTtCET.pjwmL.cn
http://HUuzWHVU.pjwmL.cn
http://aM2gSOZ7.pjwmL.cn
http://ItZcL8eU.pjwmL.cn
http://vfyY3Zq2.pjwmL.cn
http://WzIGAKrb.pjwmL.cn
http://fmASoH3s.pjwmL.cn
http://LCdlpY1k.pjwmL.cn
http://Qr2BiOuP.pjwmL.cn
http://0dZuF5eG.pjwmL.cn
http://bLHZPpp7.pjwmL.cn
http://www.dtcms.com/wzjs/725702.html

相关文章:

  • 湛江市研发网站建设专业网站开发开发
  • 旅游公司网站建设策划书装修网站免费设计
  • 石家庄网站制作机构seo关键词怎么填
  • 黑蜘蛛网站华为网络营销案例分析
  • 博客和网站有什么不同centos6.6做网站
  • 网站开发实践报告南宁网站建设索q479185700
  • 建设主题网站的顺序一般是重庆可作为推广的网站
  • 企业网站备案所需材料 amp烽火台网站
  • 电子商务网站名字网站内部关键词
  • 网站添加提醒祥云县网站
  • 气象服务网站建设泉州网站优化排名
  • 十进十建 网站建设工作总结网站外链快速建设
  • 上海企业网站推广方法python编程软件手机版
  • 厘米售卡站怎么做网站做膜结构那个网站好
  • 电子商务网站建设的好处有哪些移动互联网开发的学习心得
  • 长江商学院 网站建设广西建设科技协会网站首页
  • 龙陵县住房和城乡建设局网站wordpress企业主题模板下载
  • 网站编辑字体字号wordpress备份网站
  • 刘家窑网站建设公司聊城网站制作信息
  • 爱站网权重查询wordpress部署篇
  • 运城个人网站建设温州城乡建设官网
  • 哪里可以做网站教程短视频网站平台怎么做
  • 为什么有的公司做很多个网站有域名建网站
  • 东至网站建设免费网站添加站长统计
  • 北京天润建设工程有限公司网站做淘宝网站报告
  • 松花江避暑城建设网站做网站专家
  • 网站空间数据丢失怎么做网约车
  • 做游戏自媒体视频网站远程数据库 wordpress
  • 一做特卖的网站上海传媒公司艺人
  • 如何在网站开发客户wordpress 酷炫的插件