约定优于配置(Convention over Configuration)

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

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

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

 

<think>好的,我现在需要回答用户关于Spring框架基础介绍及其与Spring Boot关系的问题。首先,我应该回顾Spring框架的核心概念,比如IoC、AOP等,这些是Spring的基础。然后,解释Spring Boot的作用,它如何简化Spring应用的配置和部署。接着需要比较两者的关系,指出Spring Boot是建立在Spring之上,提供了默认配置和自动化设置,减少开发者的工作量。 可能用户之前接触过Spring Boot,但想更深入理解其底层框架。因此,我需要确保基础概念清晰,同时明确两者的区别和联系。要注意使用中文,并且按照要求,行内数学公式用$...$,独立公式用$$...$$,不过这个问题可能涉及较少的数学公式,但如果有相关的术语或表达式,比如依赖注入的示例,可能需要用代码块或特定格式。 需要检查是否有引用标记的需求。用户提供的引用[1]和[2]是关于Spring Boot的,但用户现在的问题是Spring框架和Spring Boot的关系,所以可能需要将引用整合到回答中,但要注意引用的相关性。例如,引用[2]提到Spring Boot集成了其他Spring组件,可以用于说明Spring Boot如何简化Spring的使用。 另外,用户要求生成相关问题,我需要根据内容提出几个相关问题,比如Spring的核心模块、Spring Boot的自动配置原理等。确保问题与主题相关,帮助用户进一步学习。 最后,结构要清晰,分点讲解,先介绍Spring,再讲Spring Boot,最后说明两者的关系。避免使用复杂术语,保持解释简洁明了。同时,确保LaTeX语法正确,虽然这个问题可能较少涉及数学公式,但如果有代码示例,需要用正确的格式。例如,提到依赖注入时,可以给出简单的代码示例,用代码块展示。</think>### Spring框架基础介绍 **Spring框架**是Java平台的开源全栈应用程序框架,主要提供**控制反转(IoC)**和**面向切面编程(AOP)**的核心功能,用于构建企业级应用。 1. **核心特性** - **控制反转(IoC)**:通过依赖注入(DI)管理对象生命周期和依赖关系,例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; // 依赖自动注入 } ``` - **面向切面编程(AOP)**:通过切面实现日志、事务管理等横切关注点,例如使用`@Transactional`注解管理事务。 - **模块化设计**:包含Spring MVC(Web开发)、Spring Data(数据访问)、Spring Security(安全)等模块[^2]。 2. **传统Spring的痛点** 早期Spring需要手动配置XML文件,依赖管理和项目搭建复杂,例如: ```xml <!-- 传统XML配置示例 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="jdbc:mysql://localhost/db"/> </bean> ``` --- ### Spring Boot与Spring的关系 **Spring Boot**是Spring生态的扩展,旨在简化Spring应用的初始搭建和配置: 1. **核心优势** - **自动配置**:根据依赖库自动配置Bean,例如引入`spring-boot-starter-web`自动配置Tomcat和Spring MVC。 - **内嵌服务器**:无需外部部署,直接通过`main`方法启动应用。 - **简化依赖管理**:通过`starter`依赖包统一版本管理,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. **与Spring的关系** - Spring Boot基于Spring框架,提供**默认配置**和**约定优于配置**的理念,例如自动扫描`src/main/resources/application.properties`。 - Spring Boot不替代Spring,而是通过`@SpringBootApplication`注解整合了Spring核心功能和其他模块[^1]。 --- ### 关键区别总结 | 特性 | Spring Framework | Spring Boot | |---------------------|-------------------------------------------|------------------------------------------| | 配置方式 | 手动配置XML或注解 | 自动配置,支持`application.yml` | | 依赖管理 | 需显式声明每个依赖 | 通过`starter`包简化依赖 | | 部署 | 需要外部Web服务器 | 内嵌Tomcat/Jetty | | 适用场景 | 高度定制化场景 | 快速开发微服务和标准化应用 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值