1.idea create SpringBoot project
- The project should create SpringBoot idea for many people to have a dish not to operate the dish, but white is for beginners, or to explain the. Open idea, then select Spring Initializr just click next, what needs to be said here is that there is a http://start.spring.io link on this interface, this link is the official website link SpringBoot, you can go to the official website SpringBoot create a project and then import to the idea, this operation on their own Baidu, I think you can directly operate in the idea, the official website to why it? It may be too busy
- Then is fill in the relevant information, including:
- Group (organization name)
- Artifact (project name, project that is unique identifier)
- Packaging: jar (a packaging)
- Package (Package name)
- Java Version (java version of the name)
and then click on the next, the next step - Then select here depend on the modules you need, then click next
- Finally, select the location of your project locally, click Finish
2.SpringBoot the parent project
- SpringBoot project structure created following the final, open pom file
- This defines the parent of a project depends on the project SpringBoot
- Hold down the Ctrl key with the left mouse button click into
- Important to see the red box inside Inside, where the definition of a parent and the parent, for the spring-boot-dependencies, from the name you can probably see that it is dependent on the management SpringBoot
Then the second red frame content probably explained as follows:
- It defines the java compiler version 1.8
- Encoded using UTF-8 format
- Packaging operation when the configuration
- Then there is the red box below to configure plug-ins, and filtering of resources
and then Ctrl + left mouse button click on the spring-boot-dependencies, as shown in - Here is SpringBoot dependency and dependency version management, you can see the version number of the pile
- 主要使用的是properties和dependencyManagement来进行版本号和依赖的管理,这既是为什么我们在SpringBoot项目引入大部分的依赖时不用写版本号,因为这是maven的依赖传递的关系,父级已经帮你定义好了版本号了
- 另外我们也可以使用properties和dependencyManagement在我们自己的项目中自定义自己的版本号,不使用父级帮我们提供的版本号,参考如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.SpringBoot的配置文件
在SpringBoot项目中的配置文件的格式有两种properties和yml,文件的位置如下(优先级由高到低,这里解释一下优先级的意思就是,在项目加载jvm的时候会优先加载哪个位置的配置文件):
- 当前项目根目录下的 config 目录下
- 当前项目的根目录下
- resources 目录下的 config 目录下
- resources 目录下
- 另外除了SpringBoot给我们默认配的配置文件的位置,我们也可以自己指定配置文件的位置。如图所示,即为在项目的resources目录下创建一个myconfig目录,然后把application.properties文件放在这个目录下
- 另外我们也可以在启动项目的时候,指定配置文件的位置,这个的操作主要针对于已经打包好的项目,无法修改配置文件了
java -jar demo.jar --spring.config.location=classpath:/myconfig/
最后是修改application.properties/application.yml配置文件的名字,因为我们的默认配置文件的名字也可以不叫作这个默认的名字,例如修改为myconfig.properties/myconfig.yml.如图,在启动项目的时候通过spring.config.name=myconfig来指定配置文件的名字
另外在已经打包好的项目的时候也可以通过下面的启动命令来指定配置文件的名字
java -jar demo.jar --spring.config.name=myconfig
最后可以同时执行多条命令,如下:
java -jar demo.jar --spring,config.name=myconfig;spring.config.location=classpath:/myconfig/
3.SpringBoot的配置文件语法
- Syntax properties of
syntax properties is based on the form of key and value, an equal sign between the key and the value obtained by connecting, for example:person.name=李依依 person.age=18
- Injection in the value of properties, by injection @Value annotation value, but also pay attention to add comment @Component object to the Spring container management
@Component
public class Person{
@Value("${person.name}")
private String name;
@Value("${person.age}")
private Integer age;
//省略getter/setter
}
- Type-safe injecting property, in addition SpringBoot provides another way of injection, the main solution to inject a lot of property, when to write many times @Value
@Component
@PropertySource("classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Book {
private String name;
private Integer age;
//省略getter/setter
}
- yml grammar
is relatively simple and straightforward than yml The syntax of the properties, prioritize the following syntax: yml are: segmentation.
Injection array values:
type:
color:
- white
- black
This data object is bound to a bean, the following code:
@ConfigurationProperties(prefix="type")
@Component
public class Config{
private List<String> color = new ArrayList<String>()
public List<String> getColor(){
return this.color
}
}
When yml profile takes a single value, as follows
type:
color:white
The value
@Value("${type.color}")
private String color
More tutorials please pay attention: non-Coban Coban, if you think tutorial please help a praise, thank you
The last wave of java to share resources, including resources from java to develop a full set of video entry, as well as java 26 projects, resources relatively large size is probably about 290g, easily link failure, access to public concern is the number of ways: non Coban after Coban, let reply: java project you can get, I wish you all happy to learn