SpringBoot configuration file management

1. JDBC database connection configuration

spring: 
    datasource: 
        url: jdbc:mysql://localhosst:3306/ 
        name: root 
        password: ****** 
        driver-class-name: com.mysql.jdbc

2. Port and service path configuration

server: 
    port: 8090 
#配置后浏览器访问需要在端口后跟此路径才能访问接口 
    servlet: 
        context-path: /simpleTools

3. Configuration injection for single configuration item

#配置文件yml中写入 
improtParam: 10 

#代码中使用注解注入 
@value("${improtParam}") 
private int improtParam;

4. Configuration injection of multiple configuration items

#配置文件中写入 
paramObj: 
    name: 张三 
    age: 20 
    description: my name is ${paramObj.name}, I age ${age} year old. 
#代码中创建配置类对象 
@Component//springBean注入 
@ConfigurationProperties(prefix = "paramObj") 
@data 
public class ParamObj{ 
    private String name; 
    private int age; 
    private description; 
} 

#代码中注入使用 
@Autowired 
private ParamObj paramObj;

5. Specify startup with multiple configuration files.

#默认yml文件 文件名:application.yml 
spring: 
    profiles: 
        active: dev

 

#开发配置文件 文件名:application-dev.yml 
spring: 
    datasource: 
        url: jdbc:mysql://localhosst:3306/ 
        name: root 
        password: ****** 
        driver-class-name: com.mysql.jdbc 
paramObj: 
    name: dev开发 
    age: 20 
    description: my name is ${paramObj.name}, I age ${age} year old.
#开发配置文件 文件名:application-test.yml 
spring: 
    datasource: 
        url: jdbc:mysql://localhosst:3306/ 
        name: root 
        password: ******
        driver-class-name: com.mysql.jdbc 
paramObj: 
    name: test测试 
    age: 30 
    description: my name is ${paramObj.name}, I age ${age} year old.

6. Maven operation commands

//1、打包,进入项目根目录,打开cmd。输入命令 
mvn clean package 

//2、运行,target为jar包打包的默认目录 
java -jar target/xxx-0.0.1-SNAPSHOT.jar
 
//3、运行时指定配置文件 
java -jar -Dspring.profiles.active=test target/xxx-0.0.1-SNAPSHOT.jar

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_42080073/article/details/105610779