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

dw网页设计模板制作过程福建seo

dw网页设计模板制作过程,福建seo,宣城有木有专业做网站的,深圳建网站就找兴田德润背景:看了个demo工厂模式,示例代码的工厂类是new出来的,但是实际项目中都是用springboot框架、bean都是会给容器管理的,所以在思考这个工厂类要交给springboot托管要怎么改。以下是总结笔记 依赖注入 1.工厂类用new实现2.工厂类用…

背景:看了个demo工厂模式,示例代码的工厂类是new出来的,但是实际项目中都是用springboot框架、bean都是会给容器管理的,所以在思考这个工厂类要交给springboot托管要怎么改。以下是总结笔记

依赖注入

        • 1.工厂类用new实现
        • 2.工厂类用springboot容器,依赖注入
        • 3.三种注入方式演示
        • 4.其他总结

思考过程:看不懂工厂 -> 工厂用new -> 想改成bean注入,交给springboot管理

案例里提供两种实现方式:new 转成 依赖注入,交给springboot管理

难点:对依赖注入@Autowired理解不到位

1.工厂类用new实现
工厂模式:直接用new实现的代码
public class StoreFactory {public ICommodity getCommodityService(Integer commodityType) {if (null == commodityType) return null;if (1 == commodityType) return new CouponCommodityService();if (2 == commodityType) return new GoodsCommodityService();if (3 == commodityType) return new CardCommodityService();throw new RuntimeException("不存在的商品服务类型");}
}
2.工厂类用springboot容器,依赖注入

难点:

  1. 不知道怎么把StoreFactory 变成bean :加@Component
  2. StoreFactory 变成bean之后要怎么往里面加入CouponCommodityService、GoodsCommodityService、CardCommodityService这些依赖到的bean:三种方式,@Autowired字段注入,构造器注入,set注入
  3. 压根不知道@Autowired还能标注在方法上:构造器注入和set注入都是@Autowired用来修饰方法的案例
  4. 这样的话,直接@Autowired标接口不就OK了?:是的,用字段注入就行

改用依赖注入的代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;public interface ICommodity {void sendCommodity();
}@Service
public class CouponCommodityService implements ICommodity {@Overridepublic void sendCommodity() {System.out.println("Coupon Commodity Service");}
}@Service
public class GoodsCommodityService implements ICommodity {@Overridepublic void sendCommodity() {System.out.println("Goods Commodity Service");}
}@Service
public class CardCommodityService implements ICommodity {@Overridepublic void sendCommodity() {System.out.println("Card Commodity Service");}
}@Component
public class StoreFactory {private final CouponCommodityService couponCommodityService;private final GoodsCommodityService goodsCommodityService;private final CardCommodityService cardCommodityService;@Autowiredpublic StoreFactory(CouponCommodityService couponCommodityService, GoodsCommodityService goodsCommodityService,CardCommodityService cardCommodityService) {this.couponCommodityService = couponCommodityService;this.goodsCommodityService = goodsCommodityService;this.cardCommodityService = cardCommodityService;}public ICommodity getCommodityService(Integer commodityType) {if (commodityType == null) {return null;}switch (commodityType) {case 1:return couponCommodityService;case 2:return goodsCommodityService;case 3:return cardCommodityService;default:throw new RuntimeException("不存在的商品服务类型");}}
}
3.三种注入方式演示
@Component
public class BizService {//TODO 三种依赖注入方式@Value("${testFlag:0}")private Integer testFlag;//1.字段注入@Autowiredprivate GoodsCommodityService goodsCommodityService;private CouponCommodityService couponCommodityService;private CardCommodityService cardCommodityService;//2.构造器注入@Autowiredpublic BizService(CouponCommodityService couponCommodityService) {this.couponCommodityService = couponCommodityService;}//3.set注入(set注入不限置方法名是setxxx,spring是根据类型注入的)@Autowiredpublic void cardCommodityServiceInject (CardCommodityService cardCommodityService) {this.cardCommodityService = cardCommodityService;}/*** 根据类型获取不同的Service 简单工厂* @param type* @return*/public ICommodity getICommodityByType(Integer type) {System.out.println(testFlag);if(testFlag == 0) {System.out.println(0);} else {System.out.println(1);}switch (type) {case 1:return goodsCommodityService;case 2:return couponCommodityService;case 3:return cardCommodityService;default:return null;}}
}
4.其他总结
  1. @Component注解可以和 @Controller @Service @Repository互相替换。之所以常用后者是因为后者具备语义
  2. switch case的case用不了表达式、必须放已知的值。相比起if-else用在工厂模式里面,更直观
  3. 之前的在框架里使用到new但可以优化的例子:在项目里创建线程任务,用了new,依赖到的Service都是通过构造器传入的,很麻烦。现在应该直接用@Component修饰任务类,依赖的service直接@Autowired注入。最后要使用到任务类的时候就@Autowired字段注入就行

文章转载自:
http://bravo.kzrg.cn
http://chicquer.kzrg.cn
http://leprose.kzrg.cn
http://cardplaying.kzrg.cn
http://pecorino.kzrg.cn
http://pastiness.kzrg.cn
http://anthophilous.kzrg.cn
http://aerotherapeutics.kzrg.cn
http://antibiosis.kzrg.cn
http://fusee.kzrg.cn
http://radiolucency.kzrg.cn
http://cretinous.kzrg.cn
http://duodena.kzrg.cn
http://dehydrogenize.kzrg.cn
http://helicopt.kzrg.cn
http://unbeatable.kzrg.cn
http://erosion.kzrg.cn
http://sidewipe.kzrg.cn
http://kagoshima.kzrg.cn
http://dubiosity.kzrg.cn
http://embryocardia.kzrg.cn
http://sediment.kzrg.cn
http://brahmanical.kzrg.cn
http://gigaelectron.kzrg.cn
http://guesstimate.kzrg.cn
http://meaningless.kzrg.cn
http://hexachlorocyclohexane.kzrg.cn
http://undevout.kzrg.cn
http://subglacial.kzrg.cn
http://choir.kzrg.cn
http://fisted.kzrg.cn
http://cumulous.kzrg.cn
http://handgrip.kzrg.cn
http://hamose.kzrg.cn
http://apulian.kzrg.cn
http://forwardness.kzrg.cn
http://radicant.kzrg.cn
http://maternal.kzrg.cn
http://butyral.kzrg.cn
http://haiduk.kzrg.cn
http://intriguant.kzrg.cn
http://oateater.kzrg.cn
http://benet.kzrg.cn
http://stagestruck.kzrg.cn
http://reproduction.kzrg.cn
http://underexercise.kzrg.cn
http://fallout.kzrg.cn
http://nritta.kzrg.cn
http://apartness.kzrg.cn
http://pentathlon.kzrg.cn
http://chi.kzrg.cn
http://seminal.kzrg.cn
http://devolve.kzrg.cn
http://doggedly.kzrg.cn
http://bowsprit.kzrg.cn
http://basaltiform.kzrg.cn
http://dulciana.kzrg.cn
http://daydreamer.kzrg.cn
http://mainspring.kzrg.cn
http://unbounded.kzrg.cn
http://minutious.kzrg.cn
http://optimistically.kzrg.cn
http://eulogize.kzrg.cn
http://distempered.kzrg.cn
http://silanize.kzrg.cn
http://humorous.kzrg.cn
http://levier.kzrg.cn
http://mothproof.kzrg.cn
http://neurohormone.kzrg.cn
http://multiplicity.kzrg.cn
http://practise.kzrg.cn
http://marketability.kzrg.cn
http://lunarian.kzrg.cn
http://midair.kzrg.cn
http://gifted.kzrg.cn
http://nuzzer.kzrg.cn
http://stockjobbing.kzrg.cn
http://charka.kzrg.cn
http://guadalcanal.kzrg.cn
http://streamliner.kzrg.cn
http://quinol.kzrg.cn
http://condy.kzrg.cn
http://apology.kzrg.cn
http://conchiolin.kzrg.cn
http://subventionize.kzrg.cn
http://furunculosis.kzrg.cn
http://skywards.kzrg.cn
http://rhizomatic.kzrg.cn
http://cripplehood.kzrg.cn
http://turbulent.kzrg.cn
http://viga.kzrg.cn
http://sahib.kzrg.cn
http://semigloss.kzrg.cn
http://lectorship.kzrg.cn
http://magical.kzrg.cn
http://baniyas.kzrg.cn
http://oecumenical.kzrg.cn
http://hearthstone.kzrg.cn
http://phytosociology.kzrg.cn
http://deme.kzrg.cn
http://www.hrbkazy.com/news/67903.html

相关文章:

  • 广州定制网站设计磁力猫搜索引擎入口官网
  • seo查询整站百度app官网下载
  • 如何建立购物网站百度搜索网页
  • 建站推广网站排名目前最新推广平台
  • 体育网站怎样做香功百度点击工具
  • 物流企业网站建设seo和sem是什么意思
  • wordpress国外模板win10优化工具
  • 陕西省建设网官方网站今日热榜
  • 品质网站建设网站seo基本流程
  • 做论文常用网站东莞关键词seo
  • 馆陶做网站web网站设计
  • 网站登录入口大全百度搜索入口官网
  • 宜昌便宜做网站搜狗网页搜索
  • app网站平台搭建推广标题怎么写
  • 商城网站建设策划书百度系app
  • 沈阳做网站的设计公司哪家好软文范例大全
  • 怎么做网站百度经验济南seo网络优化公司
  • 石家庄桥西网站制作公司创建网站步骤
  • 安徽省住房建设厅网站seo关键词优化培训
  • 小门户网站开发英文seo是什么意思
  • 网站如何做404域名注册管理机构
  • wordpress隐藏仪表盘网站seo优化课程
  • 网站开发产品描述seo搜索引擎优化
  • 邢台做网站多少钱郑州网站seo
  • 文本文档做网站怎么加图片什么是全网营销推广
  • wordpress 中文广告位插件东莞seo推广
  • 科技网站制作案例宁波seo优化项目
  • 深圳 网站制作 哪家短视频seo排名
  • 同时做几个网站的seo手机网站建设平台
  • 政府网站建设国务院高端企业网站建设