1,为什么要使用profiles 在开发中,一般有多种环境,例如:
生产环境
开发环境
有时候开发环境和生产环境的配置方法是不一样的,那么如何快速的切换呢,这里就要使用profiles文件
2. 使用方法 1,创建applicatin-dev.properties
2,创建applicatin-pro.properties
3,修改application.properties
1 2 spring.profiles.active =prod
spring.profiles.active=dev
:
1 2021-03-25 12:13:36.722 INFO 11064 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path ''
spring.profiles.active=prod
:
1 2021-03-25 12:12:28.874 INFO 10972 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8020 (http) with context path ''
application.properties即使设置了server.port=8080
,激活dev/prod,最终加载的是dev/prod的配置。
3. 注解使用方法 DataSource.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.example.demo.vo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data @NoArgsConstructor @AllArgsConstructor public class DataSource { private String driverClassName; private String url; private String username; private String password; }
DataSourceConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package com.example.demo.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Profile;import com.example.demo.vo.DataSource;@Configuration public class DataSourceConfig { @Bean @Profile(value="dev") public DataSource getDataSourceDev () { DataSource dataSource = new DataSource (); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver" ); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" ); dataSource.setUsername("root" ); dataSource.setPassword("123456" ); return dataSource; } @Bean @Profile(value="prod") public DataSource getDataSourceProd () { DataSource dataSource = new DataSource (); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver" ); dataSource.setUrl("jdbc:mysql://192.168.10.132:3306/ping?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC" ); dataSource.setUsername("root" ); dataSource.setPassword("dv*3s;Qdc]p" ); return dataSource; } }
application.properties
1 2 3 4 spring.profiles.active =prod
ApplicationTests.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.example.demo;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import com.example.demo.vo.DataSource;@SpringBootTest class ApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads () { System.out.println(dataSource); } }
4. 去掉application.properties的运行方式 项目右键->Run As
->Run Configuartions...
,
在VM arguments输入:
1 -Dspring.profiles.active=dev
1 2021-03-25 12:19:20.959 INFO 6148 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path ''
5. 去掉application.properties的jar包运行方式 用maven把项目打成jar包,
1 java -jar 04_springboot_config_profiles_my-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
output:
1 2021-03-25 12:45:51.352 INFO 14208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8020 (http) with context path ''
6. yml文件的多配置块的集成切换 application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 spring: profiles: active: - dev server: port: 8090 --- spring: profiles: dev server: port: 8010 --- spring: profiles: prod server: port: 8020
1 2 3 4 5 6 spring: profiles: active: - dev
1 2 3 4 5 6 spring: profiles: active: - prod