Little knowledge, big challenge! This article is participating in the creation of " Tips for Programmers ".
Wikipedia explains
Wikipedia explains it as follows:
Convention over configuration , also known as programming by convention , is a software design paradigm that aims to reduce the number of decisions software developers need to make, and to live the benefits of simplicity without losing flexibility.
Essentially, the developer only needs to specify the parts of the application that don't conform to the conventions. For example, if there is a class named Sales in the model, the corresponding table in the database will be named sales by default. Only if you deviate from this convention, such as naming the table "products_sold", do you need to write configuration about this name.
If the conventions of the tool you are using match your expectations, you can omit the configuration; otherwise, you can configure it the way you want.
personal understanding
Convention is better than configuration. It is not zero configuration or no configuration at all, but to reduce configuration through convention.
Convention is better than configuration. It is not a new routine, new technology, or new idea, but it has always existed. Spring Boot just amplifies it and truly achieves convention over configuration.
For example, in Spring Boot, when we import spring-boot-starter-web
a , it will automatically help us import related dependencies of Spring MVC (including Jackson supported by Json and Hibernate Validator of data validation) and a built-in Tomcat container, which makes the development Stages can run a Web project independently, either directly through the main method or as a JAR package.
Spring Boot agrees, when you spring-boot-starter-web
import , it agrees that you are a web development environment, and when you are a web environment, it agrees that you will use Spring MVC (Struts2 and so on, goodbye, because it is not your own, and it is true There is no high usage rate of Spring MVC) As for others, you will need them, and they will be imported by default. When you feel inappropriate, you can use fewer changes to meet your needs.
Spring is promoting the design concept of "convention is better than configuration", starting from Spring's annotation version (released in JDK5.0, using metadata, and introducing the concept of annotation). The introduction of annotations is to reduce some default configurations, and the introduction of annotations also represents the beginning of simplified configuration. Officially, the basis of spring is this fact.
SpringBoot agreed to reduce dependencies in the form of starters, so many commonly used starters have been launched one after another.
How to reflect in Spring Boot
Operation mode
spring-boot-starter-web
It contains the related dependencies of Spring MVC (including Jackson supported by Json and Hibernate Validator of data validation) and a built-in Tomcat container, which makes it possible to run a WEB project independently through the main method or JAR package during the development phase. In the deployment stage, it can also be marked as a WAR package and run on the production environment.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StorageApplication {
public static void main(String[] args) {
SpringApplication.run(StorageApplication.class, args);
}
}
复制代码
In an @SpringBootApplication
annotated class, use SpringApplication
the run
method to move items through the JAR.
Inherit the SpringBootServletInitializer
class and implement theconfigure
method to start the project through the WAR.application
sources
configuration file
Spring Boot's configuration file must be, and can only be, a application.
named yml
file or properties
file , and it is unique.
By default, Spring Boot will only go src-main-resources
to the folder to find the application
configuration file.
spring.profiles.active
A property called can be defined in the configuration file, which can read different configuration files according to different operating environments. For example dev
, , Spring Boot will additionally application-dev.yml
read the configuration of the environment from the configuration file.
There are two ways for Spring Boot to inject configuration file properties. One is to accept the properties in the configuration file through @Value
annotations , and the other is to use @ConfigurationProperties
annotations to automatically inject corresponding properties into beans through the set method.
- By
@Value
injecting properties, the receiver can be either a method parameter or a member variable, for example:
/**
* elasticsearch的地址
*/
@Value("${elasticsearch.host}")
private String host;
复制代码
- By initializing the bean by
@ConfigurationProperties
reading the configuration, the corresponding set method injection will be called directly:
@Bean(name = "account")
@ConfigurationProperties(prefix = "spring.datasource.account")
public DataSource accountDbDataSource() {
return DataSourceBuilder.create().build();
}
复制代码
DataSource
If used Spring-boot-starter-data-jpa
, Spring Boot will automatically create a DataSource Bean. Its properties can be defined directly in the configuration file, prefixed with Spring.datasource
. And there is no need to specify the dialect of the database, this bean will automatically determine which database to use according to the database driver that the project always depends on.
Similarly, if used spring-boot-starter-data-redis
, it will also be automatically created RedisTemlete
, ConnectionFactory
etc. Bean. Properties can also be defined in configuration files, prefixed with spring.redis
.