Multi-environment Configuration _springboot springboot Common configuration

Source: Internet
Author: User

In the previous lesson we built a Springboot project with no configuration on the idea tool and started it successfully, but we are all aware that these are far from meeting our actual project requirements, such as introducing our own Redis configuration, MySQL configuration, What should be done about it. In spring MVC we are all through the Spring.xml related file configuration, in Springboot these are no longer exist, how should we configure it. Don't worry, let's find out the answer for everyone, follow me.

NO1. Do we make a difference in the environment when we do the project? For example, development environment, test environment, production environment, and so on, then the first step I will first take everyone to configure the environment;

1. First open the Pom.xml file of our project to add the following content:

<build> <finalName>${project.artifactId}-${project.version}</finalName> <plugins> ;p lugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven-
                  plugin</artifactid> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </exe
         cutions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> &L
            T;configuration> <source>1.8</source> <target>1.8</target> <encoding>utf8</encoding> </configuration> </plugin> </plugins> <f Ilters> <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</exclud e> <exclude>application-prod.properties</exclude> </excludes> &LT;/RESOURC e> <resource> <directory>src/main/resources</directory> <filtering>true </filtering> <includes> <include>application-${filter-resource-name}.properties< /include> </includes> </resource> </resources> </build> <profiles> <profile> <id&gt ;d ev</id> <activation> <activeByDefault>true</activeByDefault> </activatio n> <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> &LT;FILTER-RESOURCE-NAME&GT;ALPHA&LT;/FILTER-RESOURCE-NAME&G
      T
         </properties> </profile> <profile> <id>prod</id> <properties> <filter-resource-name>prod</filter-resource-name> </properties> </profile> </profi Les>

This section believes that everyone is very familiar with it, I do not have to do more explanation (have the question of children's shoes can dms me OH);

2. Then open the Application.properties file and add the following content to it:

#表示激活的配置文件 (Dev|prod)
spring.profiles.active= @filter-resource-name@

The entire project becomes the following structure:


At this point our springboot multi-environment configuration has been completed;

3. Set the log level

#log level
Logging.level.root=debug

4. Set up custom port and instance name

#端口
server.port=8888

#实例名
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 "class=" Ch.qos.logback.core.rolling.RollingFileAppender " > <file>demo/demo.log</file> <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRoll Ingpolicy "> <!--Roll back daily--> <filenamepattern>demo/demo.%d{yyyy-mm-dd}.log</filenam epattern> <!--log max history 10 days--> <maxHistory>10</maxHistory> </rollingpolicy > <encoder charset= "UTF-8" > <pattern>${FILE_LOG_PATTERN}</pattern> </encoder&
   Gt </appender> <logger name= "Com.example.demo" level= "INFO" additivity= "false" > <appender-ref ref= "D Emo "/> </logger> <logger name=" Com.example.demo.dao "level=" DEBUG "/>, <logger name=" Com.exampl E.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" ERROR "level= <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 environmental configuration of our project has been set up, through the Maven clean install below Select Dev|test|prod to enter your specified configuration, and then run application runs, if through localhost : 8888 can visit the description of your configuration worked, but this is far from enough, our project development must operate the database, haha yes, next let us enter the Springboot + MySQL + mybatis world bar.  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.