在上一课中我们通过idea工具没有做任何配置就构建了一个springboot项目,并且已经成功启动了,但我们都很清楚这些都远远不能达到我们实际项目的需求,比如我们要引入我们自己的redis配置、mysql配置等,应该如何处理呢?在spring mvc中我们都是通过spring.xml相关文件配置,在springboot中这些都已经不存在了,我们应该怎样配置呢?别急,马上为大家揭晓谜底,跟着我一起来吧!
no1.我们在做项目的时候是不是都会区分很多环境呢?比如开发环境、测试环境、生产环境等,那么第一步我将先带大家配置好各个环境;
1.首先打开我们项目的pom.xml文件加入以下内容:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<build> <finalname>${project.artifactid}-${project.version}</finalname> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version> 3.3 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> <encoding>utf8</encoding> </configuration> </plugin> </plugins> <filters> <filter>src/main/resources/application-${filter-resource-name}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering> true </filtering> <excludes> <exclude>filters/*</exclude> <exclude>filters/*</exclude> <exclude>application-dev.properties</exclude> <exclude>application-test.properties</exclude> <exclude>application-alpha.properties</exclude> <exclude>application-prod.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering> true </filtering> <includes> <include>application-${filter-resource-name}.properties</include> </includes> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <activation> <activebydefault> true </activebydefault> </activation> <properties> <filter-resource-name>dev</filter-resource-name> </properties> </profile> <profile> <id>test</id> <properties> <filter-resource-name>test</filter-resource-name> </properties> </profile> <profile> <id>alpha</id> <properties> <filter-resource-name>alpha</filter-resource-name> </properties> </profile> <profile> <id>prod</id> <properties> <filter-resource-name>prod</filter-resource-name> </properties> </profile> </profiles> |
这一段相信大家都很熟悉了吧,我就不多做解释了(有疑问的童鞋可以私信我哦);
2.然后打开application.properties文件,并在其中加入以下内容:
#表示激活的配置文件(dev|prod)
spring.profiles.active=@filter-resource-name@
整个项目变成了如下结构:
至此我们的springboot多环境配置已经完成;
3.设置日志级别
1
2
|
#log level logging.level.root=debug |
4.设置自定义端口以及实例名
1
2
3
4
|
#端口 server.port= 8888 #实例名 spring.application.name=demo-springboot |
5.logback-spring.xml
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <configuration> <include resource= "org/springframework/boot/logging/logback/base.xml" /> <appender name= "demo" class = "ch.qos.logback.core.rolling.rollingfileappender" > <file>demo/demo.log</file> <rollingpolicy class = "ch.qos.logback.core.rolling.timebasedrollingpolicy" > <!-- 按天回滚 daily --> <filenamepattern>demo/demo.%d{yyyy-mm-dd}.log</filenamepattern> <!-- 日志最大的历史 10 天 --> <maxhistory> 10 </maxhistory> </rollingpolicy> <encoder charset= "utf-8" > <pattern>${file_log_pattern}</pattern> </encoder> </appender> <logger name= "com.example.demo" level= "info" additivity= "false" > <appender-ref ref= "demo" /> </logger> <logger name= "com.example.demo.dao" level= "debug" /> <logger name= "com.example.demo.service" level= "info" /> <logger name= "druid.sql.statement" level= "debug" /> <logger name= "druid.sql.resultset" level= "debug" /> <logger name= "org.apache" level= "info" /> <logger name= "org.mybatis.spring" level= "error" /> <logger name= "org.springframework" level= "info" ></logger> <logger name= "springfox" level= "error" ></logger> <root level= "info" > <appender-ref ref= "demo" /> </root> </configuration |
至此,我们项目的基本环境配置已经搭建好,通过maven clean install以下选择dev|test|prod打入你指定的配置,然后run application运行,如果通过localhost:8888可以访问说明你的配置worked了;但是这还远远不够,我们项目开发总得操作数据库吧,哈哈 是的,接下来让我们进入springboot + mysql + mybatis的世界吧!
总结
以上所述是小编给大家介绍的springboot 多环境配置教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/zwx19921215/article/details/79786778