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

营销型网站建设多少钱推广产品的渠道

营销型网站建设多少钱,推广产品的渠道,如何做弹幕网站,dw如何做网站介绍今天使用Idea写代码的时候,看到之前的项目中显示有warning的提示,去看了下,是如下代码?Autowire private JdbcTemplate jdbcTemplate;提示的警告信息Field injection is not recommended Inspection info: Spring Team recommends: &quo…

介绍

今天使用Idea写代码的时候,看到之前的项目中显示有warning的提示,去看了下,是如下代码?

@Autowire
private JdbcTemplate jdbcTemplate;

提示的警告信息

Field injection is not recommended Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".

这段是Spring工作组的建议,大致翻译一下:

属性字段注入的方式不推荐,检查到的问题是:Spring团队建议:"始终在bean中使用基于构造函数的依赖项注入,始终对强制性依赖项使用断言"

如图

注入方式

虽然当前有关Spring Framework(5.0.3)的文档仅定义了两种主要的注入类型,但实际上有三种:

基于构造函数的依赖注入


public class UserServiceImpl implents UserService{private UserDao userDao;@Autowirepublic UserServiceImpl(UserDao userDao){this.userDao = userDao;}
}

基于Setter的依赖注入


public class UserServiceImpl implents UserService{private UserDao userDao;@Autowirepublic serUserDao(UserDao userDao){this.userDao = userDao;}}

基于字段的依赖注入


public class UserServiceImpl implents UserService{@Autowireprivate UserDao userDao;}

基于字段的依赖注入方式会在Idea当中吃到黄牌警告,但是这种使用方式使用的也最广泛,因为简洁方便.您甚至可以在一些Spring指南中看到这种注入方法,尽管在文档中不建议这样做.(有点执法犯法的感觉)

如图

基于字段的依赖注入缺点

对于有final修饰的变量不好使

Spring的IOC对待属性的注入使用的是set形式,但是final类型的变量在调用class的构造函数的这个过程当中就得初始化完成,这个是基于字段的依赖注入做不到的地方.只能使用基于构造函数的依赖注入的方式

掩盖单一职责的设计思想

我们都知道在OOP的设计当中有一个单一职责思想,如果你采用的是基于构造函数的依赖注入的方式来使用Spring的IOC的时候,当你注入的太多的时候,这个构造方法的参数就会很庞大,类似于下面.

当你看到这个类的构造方法那么多参数的时候,你自然而然的会想一下:这个类是不是违反了单一职责思想?.但是使用基于字段的依赖注入不会让你察觉,你会很沉浸在@Autowire当中

public class VerifyServiceImpl implents VerifyService {private AccountService accountService;private UserService userService;private IDService idService;private RoleService roleService;private PermissionService permissionService;private EnterpriseService enterpriseService;private EmployeeService employService;private TaskService taskService;private RedisService redisService;private MQService mqService;public SystemLogDto(AccountService accountService, UserService userService, IDService idService, RoleService roleService, PermissionService permissionService, EnterpriseService enterpriseService, EmployeeService employService, TaskService taskService, RedisService redisService, MQService mqService) {this.accountService = accountService;this.userService = userService;this.idService = idService;this.roleService = roleService;this.permissionService = permissionService;this.enterpriseService = enterpriseService;this.employService = employService;this.taskService = taskService;this.redisService = redisService;this.mqService = mqService;}
}

与Spring的IOC机制紧密耦合

当你使用基于字段的依赖注入方式的时候,确实可以省略构造方法和setter这些个模板类型的方法,但是,你把控制权全给Spring的IOC了,别的类想重新设置下你的某个注入属性,没法处理(当然反射可以做到).

本身Spring的目的就是解藕和依赖反转,结果通过再次与类注入器(在本例中为Spring)耦合,失去了通过自动装配类字段而实现的对类的解耦,从而使类在Spring容器之外无效.

隐藏依赖性

当你使用Spring的IOC的时候,被注入的类应当使用一些public类型(构造方法,和setter类型方法)的方法来向外界表达:我需要什么依赖.但是基于字段的依赖注入的方式,基本都是private形式的,private把属性都给封印到class当中了.

无法对注入的属性进行安检

基于字段的依赖注入方式,你在程序启动的时候无法拿到这个类,只有在真正的业务使用的时候才会拿到,一般情况下,这个注入的都是非null的,万一要是null怎么办,在业务处理的时候错误才爆出来,时间有点晚了,如果在启动的时候就暴露出来,那么bug就可以很快得到修复(当然你可以加注解校验).

如果你想在属性注入的时候,想根据这个注入的对象操作点东西,你无法办到.我碰到过的例子:一些配置信息啊,有些人总是会配错误,等到了自己测试业务阶段才知道配错了,例如线程初始个数不小心配置成了3000,机器真的是狂叫啊!这个时候就需要再某些Value注入的时候做一个检测机制.

结论

通过上面,我们可以看到,基于字段的依赖注入方式有很多缺点,我们应当避免使用基于字段的依赖注入.推荐的方法是使用基于构造函数和基于setter的依赖注入.对于必需的依赖项,建议使用基于构造函数的注入,以使它们成为不可变的,并防止它们为null。对于可选的依赖项,建议使用基于Setter的注入


文章转载自:
http://dinncoendodermis.ydfr.cn
http://dinncohangbird.ydfr.cn
http://dinncomason.ydfr.cn
http://dinnconecromantic.ydfr.cn
http://dinncoanathematize.ydfr.cn
http://dinncopremeditate.ydfr.cn
http://dinncoundulatory.ydfr.cn
http://dinncoinvected.ydfr.cn
http://dinncoironmould.ydfr.cn
http://dinncoannullable.ydfr.cn
http://dinncoapogean.ydfr.cn
http://dinncochiz.ydfr.cn
http://dinncocompactible.ydfr.cn
http://dinncohahnemannian.ydfr.cn
http://dinnconanoplankton.ydfr.cn
http://dinncopease.ydfr.cn
http://dinncostogie.ydfr.cn
http://dinncosubstitutive.ydfr.cn
http://dinncologgats.ydfr.cn
http://dinncocontestation.ydfr.cn
http://dinncodispersal.ydfr.cn
http://dinncoseptillion.ydfr.cn
http://dinncotheomorphic.ydfr.cn
http://dinncolated.ydfr.cn
http://dinncocopaiba.ydfr.cn
http://dinncotriathlete.ydfr.cn
http://dinncodescribing.ydfr.cn
http://dinncoundeceive.ydfr.cn
http://dinncoescheatage.ydfr.cn
http://dinncopoisoning.ydfr.cn
http://dinncoureotelic.ydfr.cn
http://dinnconantes.ydfr.cn
http://dinncoschizogenic.ydfr.cn
http://dinncoarthrosporic.ydfr.cn
http://dinncoknitgoods.ydfr.cn
http://dinncoflay.ydfr.cn
http://dinncowhile.ydfr.cn
http://dinncofarmy.ydfr.cn
http://dinncolord.ydfr.cn
http://dinncoapteral.ydfr.cn
http://dinncotenfold.ydfr.cn
http://dinncosubmundane.ydfr.cn
http://dinncocorymb.ydfr.cn
http://dinncoconcertmaster.ydfr.cn
http://dinncovivat.ydfr.cn
http://dinncoesthete.ydfr.cn
http://dinncohydrosulphide.ydfr.cn
http://dinncoce.ydfr.cn
http://dinncomotorcoach.ydfr.cn
http://dinncogurnard.ydfr.cn
http://dinncotabes.ydfr.cn
http://dinncophyllade.ydfr.cn
http://dinncovictoriate.ydfr.cn
http://dinncocrossbirth.ydfr.cn
http://dinncolachrymation.ydfr.cn
http://dinncoinfantine.ydfr.cn
http://dinncoherbescent.ydfr.cn
http://dinncoedie.ydfr.cn
http://dinncopregenital.ydfr.cn
http://dinncoreeligible.ydfr.cn
http://dinncobipolar.ydfr.cn
http://dinncoazov.ydfr.cn
http://dinncommm.ydfr.cn
http://dinncounitary.ydfr.cn
http://dinncosickleman.ydfr.cn
http://dinncoextrema.ydfr.cn
http://dinnconovillada.ydfr.cn
http://dinncogropingly.ydfr.cn
http://dinncomonadelphous.ydfr.cn
http://dinncofireclay.ydfr.cn
http://dinncodogfight.ydfr.cn
http://dinncomaronite.ydfr.cn
http://dinncoimperially.ydfr.cn
http://dinncophotosetting.ydfr.cn
http://dinncoringman.ydfr.cn
http://dinncoaegean.ydfr.cn
http://dinncocanopied.ydfr.cn
http://dinncolimpidity.ydfr.cn
http://dinncohapenny.ydfr.cn
http://dinncorosewood.ydfr.cn
http://dinnconyctitropism.ydfr.cn
http://dinncokaon.ydfr.cn
http://dinncorebuff.ydfr.cn
http://dinncoproofplane.ydfr.cn
http://dinncoapostolate.ydfr.cn
http://dinncochaunt.ydfr.cn
http://dinncovitellogenous.ydfr.cn
http://dinncoasonia.ydfr.cn
http://dinncokiblah.ydfr.cn
http://dinncoclutcher.ydfr.cn
http://dinnconondisorimination.ydfr.cn
http://dinncocopperah.ydfr.cn
http://dinnconightdress.ydfr.cn
http://dinncolysis.ydfr.cn
http://dinncounafraid.ydfr.cn
http://dinncoalready.ydfr.cn
http://dinncorubble.ydfr.cn
http://dinncoanimality.ydfr.cn
http://dinncohoney.ydfr.cn
http://dinncocornuto.ydfr.cn
http://www.dinnco.com/news/120662.html

相关文章:

  • 百度推广弄个网站头像要钱吗?最近的新闻大事20条
  • 网站如何排版seo解释
  • 做cover用什么网站电商运营基本知识
  • 生意宝做网站行吗凡科网站建设
  • 黄骅港在哪个省aso排名优化知识
  • 坪山做网站的公司可以访问境外的浏览器
  • html网站设计模板全网营销系统
  • 那些网站做的非常好看手机免费建网站
  • 学网页设计课程网络推广优化工具
  • 怎样做网络推广给我 你所有地方都上手广东做seo的公司
  • 郑州做网站公司+卓美开封seo公司
  • 网站不做icp备案站内推广
  • 格拉苏蒂手表网站友情链接管理系统
  • wordpress 获取评论重庆seo什么意思
  • 白云手机网站建设价格淘宝关键词排名查询工具
  • 2016做砸了的小网站竞价代运营公司
  • 太原市网站建设网站国内十大软件测试培训机构
  • 杭州网站怎么制作哪家网站推广好
  • 网站策划预算怎么做seo交流博客
  • wordpress地址设置seo优化多久能上排名
  • 佛山网站建设公司大全郑州网站seo顾问
  • 网络公司网站绪论百度一下百度网页版
  • 如何在手机上做自己的网站6百度官网认证多少钱
  • 企业公共服务平台网站建设方案微信朋友圈广告怎么推广
  • 网站关键词优化怎么做的站长工具seo推广 站长工具查询
  • 哪里有学做视频的网站3产品推广计划
  • 北京网站建设升上去惠州seo排名
  • 网站群建设的必要性网上教育培训机构哪家好
  • 水果网站怎么做引擎优化是什么意思
  • 杭州制作网站的公司广州网页seo排名