In the previous lesson, we built a springboot project through the idea tool without any configuration, and it has been successfully launched, but we are all very clear that these are far from meeting the needs of our actual project. For example, we want to introduce our own redis configuration, mysql configuration, etc., how should we deal with it? In spring mvc, we all configure it through spring.xml related files. In springboot, these no longer exist. How should we configure it? Don’t worry, let’s explain the answer to everyone immediately, follow me!
NO1.Will we distinguish many environments when we do projects? For example, development environment, testing environment, production environment, etc., then the first step I will take you to configure each environment;
1. First open the pom.xml file of our project and add the following content:
<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> </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> </properties> </profile> <profile> <id>test</id> <properties> <filter-resource-name>test</filter-resource-name> </properties> </properties> <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>I believe everyone is familiar with this paragraph, so I won’t explain it much (if you have any questions, please send me a private message to me);
2. Then open the application.properties file and add the following content to it:
# indicates the activated configuration file (dev|prod)
spring.profiles.active=@filter-resource-name@
The entire project has become the following structure:
At this point, our springboot multi-environment configuration has been completed;
3. Set log level
#log levellogging.level.root=debug
4. Set custom port and instance name
#Port server.port=8888#Instance name spring.application.name=demo-springboot
5.logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?><configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="demo"> <file>demo/demo.log</file> <rollingPolicy> <!-- Rollingback daily --> <fileNamePattern>demo/demo.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- The largest log history is 10 days--> <maxHistory>10</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <logger name="com.example.demo" level="INFO" addition="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.Statement" 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 At this point, the basic environment configuration of our project has been built. Select dev|test|prod to enter the configuration you specified by maven clean install, and then run the application. If you can access localhost:8888, you can access and specify your configuration. But this is far from enough. Our project development must operate the database. Haha yes, let's enter the world of springboot + mysql + mybatis!
Summarize
The above is the springboot multi-environment configuration tutorial introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!