约定优于配置(Convention over Configuration)

Spring Boot如此火爆的原因,源于它的核心设计理念--约定优于配置。那怎么理解这个设计理念呢?

百科如是说:

约定优于配置(convention over configuration),也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。

本质是说,开发人员仅需规定应用中不符约定的部分。例如,如果模型中有个名为Sale的类,那么数据库中对应的表就会默认命名为sales。只有在偏离这一约定时,例如将该表命名为"products_sold",才需写有关这个名字的配置。如果您所用工具的约定与你的期待相符,便可省去配置;反之,你可以配置来达到你所期待的方式。

例如,在知名的Java对象关系映射框架Hibernate的早期版本中,将类及其属性映射到数据库上需要是在XML文件中的描述,其中大部分信息都应能够按照约定得到,如将类映射到同名的数据库表,将属性分别映射到表上的字段。后续的版本抛弃了XML配置文件,而是使用这些恰当的约定,对于不符合这些约定的情形,可以使用Java 标注来说明(参见下面提供的JavaBeans规范)。

Definition-Convention over Configuration:

Convention over configuration is a software development approach geared toward developing programs according to typical programming conventions, versus programmer defined configurations. It enables quick and simple software creation while maintaining base software requirements.

Convention over configuration is also known as coding by convention.

Techopedia explains:

Convention over configuration relies on the development of a program through the use of an underlying language’s native procedures, functions, classes and variables. This approach reduces or eliminates the need for additional software configuration files, ultimately facilitating and expediting software development, code consistency and maintenance. However, to follow these conventions, a software developer must be acquainted with the underlying framework. 

Software frameworks that support the convention over configuration development approach include Ruby on Rails, JavaBeans and CakePHP.

John Spacey explained on simplicable website:

Convention over configuration is a coding philosophy that states that configuration should only be required when you're doing something different. Conventions can be established at the language, platform or application level to minimize repetitive configurations. For example, default directories might be assumed by a platform. A mechanism is typically provided to override such conventions.It can be said that convention over configuration is a reaction to software engineering practices that go too far in planning for change by making everything conceivable configurable. This results in a shift in a developer's time from coding to configuration. It also tends to shift errors from compile time to runtime.

Overview: Convention Over Configuration 
TypeCoding Principle
DefinitionThe principle that configuration is optional where a reasonable convention can be established.
ValueMinimizing busy workReducing runtime configuration errors
Related Concepts

Worse Is Better

Principle Of Least Astonishment

Code Smell

许多新的框架使用了约定优于配置的方法,包括:SpringRuby on Rails,Kohana PHP,Grails,Grok,Zend FrameworkCakePHPsymfonyMavenASP.NET MVCWeb2py(MVC),Apache Wicket

这是一个古老的概念, 甚至在Java类库中也可以找出这一概念的踪迹。例如,JavaBean规范非常多的依赖这一概念。下面摘录JavaBeans 1.1版规范的一段:

按照一般的规则,我们不希望造出一个奇怪的java.beans.everything类,其他类需要从该类派生。而是希望在运行时JavaBeans为一般的对象提供缺省的行为特征,但是允许对象通过继承特定的java.beans.something接口来覆盖缺省的行为特征的一部分。

综上,解释了半天,总之一句话,就是按照团队约定的规范或惯例写代码,减少配置文件。

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值