约定优于配置(Convention over Configuration)

SpringBoot的流行源于其核心理念“约定优于配置”,旨在简化开发过程,减少不必要的配置,提高效率。通过遵循约定,开发人员仅需关注特殊需求的配置,使开发更加灵活高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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接口来覆盖缺省的行为特征的一部分。

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

 

### 约定优于配置的概念 约定优于配置Convention over Configuration, CoC)是一种软件设计范式,旨在减少开发者需要做出的决策数量,从而提高开发效率并降低复杂度。在这种模式下,默认情况下会采用一组预定义的标准或惯例,只有当这些默认设置无法满足需求时,才允许对其进行自定义调整[^4]。 这种理念的核心在于通过提供合理的默认行为来简化系统的配置过程。例如,在许多现代框架中,文件命名规则、目录结构以及类名映射等都可以遵循固定的模式自动完成绑定,无需显式的XML或其他形式的手动声明。 ### 在IT领域的应用实例 #### Ruby on Rails中的实现 Ruby on Rails是一个典型的运用了“约定优于配置”的Web应用程序框架。在这个框架里,数据库表的名字通常会被设定为模型名称的小写复数形式;控制器的动作名字往往对应视图模板的名字等等。这样的做法使得新加入项目的人员能够快速理解整个架构,并且减少了大量重复性的基础工作量[^5]。 ```ruby class UsersController < ApplicationController def show @user = User.find(params[:id]) end end ``` 以上代码片段展示了如何在一个Rails应用中创建一个简单的RESTful接口用于展示特定用户的资料。这里并没有指定路由或者视图的具体位置,因为它们已经按照既定的规约被安排好了。 #### Spring Boot中的体现 Spring Boot也广泛采纳了这一原则。比如,当你启动一个新的Spring Boot项目时,它可以根据`application.properties`文件的内容或者其他元数据推断出很多必要的依赖注入和服务注册操作。如果某些组件的位置不符合预期,则可以通过少量额外的注解来进行微调而不是完全重写逻辑链路[^6]。 ```java @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 上面的例子说明了只需几行简洁明了的Java代码即可初始化整个复杂的Spring上下文环境,这背后正是得益于大量的隐含假设和自动化处理机制的支持。 ### 总结 综上所述,“约定优于配置”不仅有助于加快产品迭代速度,还能促进团队内部的一致性和可维护性。然而值得注意的是,虽然这种方法极大地降低了入门门槛,但在面对高度定制化的要求时可能会显得不够灵活,因此实际使用过程中还需要权衡利弊作出适当的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值