spring boot环境配置

"本文介绍了如何在Spring Boot项目中配置不同的环境变量,如开发(dev)、测试(test)和生产(prod)环境,以及如何通过`Spring.profiles.active`来激活对应环境的配置。关键步骤包括创建application-{profile}
摘要由CSDN通过智能技术生成

1.配置文件名称,配置文件名必须满足application-{profile}.properties的格式:

application-dev.properties

application-prod.properties

application-test.properties

①创建配置文件并设置端口:

application-dev.properties  //开发环境配置文件

application-test.properties  //测试环境配置文件

application-prod.properties  //生产环境配置文件

②在全局配置文件application.properties中激活环境的属性:

Spring.profiles.active=dev     //激活开发环境配置文件

Spring.profiles.active=test   //激活测试环境配置文件

Spring.profiles.active=prod   //激活生产环境配置文件

Spring Boot中,可以通过编写主程序类来启动应用。在该类上使用@SpringBootApplication注解标注说明这是一个Spring Boot应用。具体的例子如下所示: ```java @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldMainApplication.class, args); } } ``` 此外,根据引用中的内容,可以在项目的pom.xml文件中配置相关的依赖项。例如,可以添加spring-boot-starter-web依赖项来启用web功能。具体的pom.xml配置如下所示: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 在Spring Boot应用中,还可以通过yml文件来配置环境。例如,可以在resources目录下创建一个名为application.yml的文件,并在其中配置端口号、上下文路径、数据源等信息。具体的配置如下所示: ```yaml server: port: 8989 servlet: context-path: /mvc spring: datasource: type: com.mchange.v2.c3p0.ComboPooledDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/hr username: root password: root freemarker: suffix: .ftl content-type: text/html charset: UTF-8 template-loader-path: classpath:/views/ devtools: restart: enabled: true additional-paths: src/main/java poll-interval: 3000 quiet-period: 1000 cache: ehcache: config: classpath:ehcache.xml mybatis: mapper-locations: classpath:/mappers/*.xml type-aliases-package: com.xxxx.springboot.po configuration: map-underscore-to-camel-case: true pagehelper: helper-dialect: mysql logging: level: com.xxxx.dao: debug ``` 最后,根据引用中的内容,还可以在项目中编写SQL映射文件来配置相关的数据库操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [springboot环境配置](https://blog.csdn.net/leaf_leaf__/article/details/126718902)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot环境配置](https://blog.csdn.net/gaozhen666/article/details/117130267)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值