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

做meta分析需注册的网站seo模拟点击软件源码

做meta分析需注册的网站,seo模拟点击软件源码,短网址生成器有哪些,深圳网站建设服务哪家有1、前言 一般我们使用枚举都是用来定义一些常量。比如我们需要一个表示订单类(pc订单、手机订单)的常量,那我们就可以使用枚举来实现,如下: AllArgsConstructor public enum OrderTypeEnum{PC("PC", "电脑端"),PHONE("PHONE", "手机端&quo…

1、前言

  一般我们使用枚举都是用来定义一些常量。比如我们需要一个表示订单类(pc订单、手机订单)的常量,那我们就可以使用枚举来实现,如下:

@AllArgsConstructor
public enum OrderTypeEnum{PC("PC", "电脑端"),PHONE("PHONE", "手机端");private String name;private String value;}

  接着再写一下根据name查找对应枚举的方法,枚举就算完成了。但比如我们的业务场景是这样的:我们有一个OrderTypeService接口及其实现类。

public interface OrderTypeService {String processPc(String order);String processPhone(String order);
}
@Service
public class OrderTypeServiceImpl implements OrderTypeService {@Overridepublic String processPc(String order) {//具体的处理pc订单的业务逻辑System.out.println("开始处理pc订单:" + order);return null;}@Overridepublic String processPhone(String order) {//具体的处理phone订单的业务逻辑System.out.println("开始处理phone订单:" + order);return null;}
}

  根据接口的方法名我们一眼就知道,processPc是用来处理PC订单的;processPhone是用来处理PHONE订单的。然后再实际调用这两个方法的地方就需要我们进行if-else的判断。这里的if-else判断的缺点是:代码扩展性很差,如果以后又增加了其它订单类型,我们还需要改这里的if-else逻辑。

        if (OrderTypeEnum.PC.equals(orderTypeEnum)) {orderTypeService.processPc("order");} else if (OrderTypeEnum.PHONE.equals(orderTypeEnum)) {orderTypeService.processPhone("order");}

2、思考

  针对以上if-else判断存在的问题,那我们能不能把调用各自业务逻辑的代码挪到枚举里呢?肯定是可以的,本期我们就来实现一下。

2.1、通过@PostConstruct注解

在枚举内定义BeanInjector,再通过PostConstruct注解的方法给枚举的orderTypeService属性注入bean。

@AllArgsConstructor
public enum OrderTypeEnum {PC("PC", "电脑端") {@Overridepublic void handle(String order) {orderTypeService.processPc(order);}},PHONE("PHONE", "手机端") {@Overridepublic void handle(String order) {orderTypeService.processPhone(order);}};private String name;private String value;protected static OrderTypeService orderTypeService;public abstract void handle(String order);@Componentpublic static class BeanInjector {@Autowiredprivate OrderTypeService orderTypeService;@PostConstructpublic void postConstruct() {OrderTypeEnum.orderTypeService = this.orderTypeService;}}
}

调用代码:

        OrderTypeEnum pcOrderType = OrderTypeEnum.PC;pcOrderType.handle("1");

输出结果:

开始处理pc订单:1

2.2、在枚举内实现ApplicationContextAware回调接口,获取到ApplicationContext,再从context里获取我们需要的bean

@AllArgsConstructor
public enum OrderTypeEnum {PC("PC", "电脑端") {@Overridepublic void handle(String order) {OrderTypeService orderTypeService = OrderTypeEnum.ApplicationContextProvider.getApplicationContext().getBean(OrderTypeService.class);orderTypeService.processPc(order);}},PHONE("PHONE", "手机端") {@Overridepublic void handle(String order) {OrderTypeService orderTypeService = OrderTypeEnum.ApplicationContextProvider.getApplicationContext().getBean(OrderTypeService.class);orderTypeService.processPhone(order);}};private String name;private String value;public abstract void handle(String order);@Componentpublic static class ApplicationContextProvider implements ApplicationContextAware {private static ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) {context = applicationContext;}public static ApplicationContext getApplicationContext() {return context;}}
}

调用代码同2.1
输出结果同2.1

2.3、还是需要实现ApplicationContextAware接口,但这次不是直接使用ApplicationContext,而是把ApplicationContext里的bean值直接注入到我们的枚举字段中。

public enum OrderTypeEnum {PC("PC", "电脑端") {@Overridepublic void handle(String order) {orderTypeService.processPc(order);}},PHONE("PHONE", "手机端") {@Overridepublic void handle(String order) {orderTypeService.processPhone(order);}};private String name;private String value;OrderTypeEnum(String name, String value) {this.name = name;this.value = value;}public abstract void handle(String order);@AutowiredOrderTypeService orderTypeService;
}
@Component
public class OrderTypeEnumInjector implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {for (OrderTypeEnum orderTypeEnum : OrderTypeEnum.values()) {applicationContext.getAutowireCapableBeanFactory().autowireBean(orderTypeEnum);}}
}

调用代码同2.1
输出结果同2.1
  这里重点是使用applicationContext.getAutowireCapableBeanFactory().autowireBean。它可以把枚举中使用了@Autowired注解的字段和applicationContext中的bean根据类型和名称做匹配,匹配到之后就会把bean注入到该字段中。从而枚举中的orderTypeService就有值了。

3、延申

  applicationContext.getAutowireCapableBeanFactory().autowireBean能否给普通的类(没有被spring管理)的字段注入bean呢?答案也是可以的,这里最好把这个普通对象定义成单例的,这样我们给字段注入一次bean就可以一直使用了,否则每次new 的时候还需要重新注入bean。

public class OrderTypeManager {private OrderTypeManager(){}private static OrderTypeManager orderTypeManager = null;public synchronized static OrderTypeManager getInstance(){if(orderTypeManager == null){orderTypeManager = new OrderTypeManager();}return orderTypeManager;}@Autowiredprivate OrderTypeService orderTypeService;public  void test(){orderTypeService.processPhone("2");}
}

  如上定义了一个单例的OrderTypeManager,但是OrderTypeManager没有被spring所管理,那我们怎么把OrderTypeService这个bean注入到orderTypeService字段里呢?

@Component
public class OrderTypeEnumInjector implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {for (OrderTypeEnum orderTypeEnum : OrderTypeEnum.values()) {applicationContext.getAutowireCapableBeanFactory().autowireBean(orderTypeEnum);}OrderTypeManager orderTypeManager = OrderTypeManager.getInstance();applicationContext.getAutowireCapableBeanFactory().autowireBean(orderTypeManager);}
}

调用代码:

        OrderTypeManager orderTypeManager = OrderTypeManager.getInstance();orderTypeManager.test();

输出结果:

开始处理phone订单:2
http://www.mnyf.cn/news/47842.html

相关文章:

  • 做英文网站的流程网站推荐
  • 南宁网站外包深圳市企业网站seo
  • 深圳做网站应该怎么做文大侠seo博客
  • 怎么用wordpress打开网站吗互联网广告怎么做
  • 网站优化外包费用网络营销的认识与理解
  • 成都餐饮小程序开发华为seo诊断及优化分析
  • 彩票网站建设基本流程域名关键词排名查询
  • 网站开发的经验技巧百度近日收录查询
  • 在微信上做彩票网站吗福建seo优化
  • 24小时免费看b站下载看关键词优化工具有哪些
  • 怎么修复网站死链合肥网络优化公司有几家
  • 新网站怎么做搜索引擎优化好做吗
  • 最先进的深圳网站建设百度广告联盟一个月能赚多少
  • 电脑网站和手机网站怎么做相同路径上海seo排名
  • 楚雄网站建设rewlkj网站打开速度优化
  • 公司做网站可以永久买断吗合肥关键词排名推广
  • 做网站开发有前途么镇江市网站
  • 长沙网站外包微信推广方法
  • 网站建设实训目的百度提交链接
  • 长沙市住房和城乡建设委员会网站排名优化seo公司
  • 医院建设官方网站必要性代运营哪家公司最靠谱
  • 做网站不会P图怎么办电商运营模式
  • 北京网站建设方案软件北京网络营销公司排名
  • ps做的网站模板网络营销的发展现状如何
  • 无需本金十分钟赚800兰州seo培训
  • 利用网盘做网站网站建设7个基本流程
  • html网站支付链接怎么做的如何建立免费个人网站
  • 深圳建站公司兴田德润电话多少外贸推广营销公司
  • 郑州专业网站制作建设怎样做网站卖自己的产品
  • wordpress简约新闻自媒体主题seo公司厦门