Home  >  Article  >  Java  >  Detailed introduction of SpringBoot loading submodule configuration file (code example)

Detailed introduction of SpringBoot loading submodule configuration file (code example)

不言
不言forward
2019-02-16 13:47:484915browse

This article brings you a detailed introduction (code example) about SpringBoot loading submodule configuration files. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I started to learn the SpringBoot framework in the past two days. According to the official documentation, I easily started the single-module project. However, when using maven to build multiple modules, I encountered the problem that the submodule configuration file was not loaded.

The project structure is like this

    zero
        |-ws
            |-service
                |-dao
                    |-entity

zero’s dependencies

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

ws’s dependencies and configuration

<dependencies>
    <dependency>
      <groupId>cn.xmcui.zero</groupId>
      <artifactId>service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <!--指定该class为全局唯一入口-->
          <mainClass>cn.xmcui.zero.Application</mainClass>
          <fork>true</fork>
          <layout>ZIP</layout>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

ws’s application.yml

server:
  port: 80
  servlet:
    session:
      timeout: 60
  tomcat:
    uri-encoding: utf-8

dao dependencies and configuration

<dependencies>
    <dependency>
      <groupId>cn.xmcui.zero</groupId>
      <artifactId>entity</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
  </dependencies>

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/zero?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: cn.xmcui.zero.entity

Add annotations to the launcher class

@SpringBootApplication(scanBasePackages = "cn.xmcui.zero")
@MapperScan(basePackages = "cn.xmcui.zero.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Run

Then a welcome error

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: &#39;url&#39; attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

The configuration file of the database was not found

The process of finding the error was very painful. I searched for a lot of information and took many detours. Finally, I cut all the application.yml of the dao layer to the application of ws. In .yml, the project lights up and runs successfully. This means that the configuration file of the dao layer has not been loaded.

Then I found a way to load the configuration file:

I will ws layer application Rename .yml to application-dev.yml; rename the dao layer configuration file to application-dao.yml (so that the configuration files do not have the same name, please note that the configuration file must be prefixed with application- after renaming);

Create a new application.yml in the ws layer

spring:
  profiles:
    active: dao,dev

This configuration specifies which configuration files to load

The operation is completed and the system lights up successfully

It was originally A very simple question, but it took me a long time. There is one more thing I must complain about. The quality of SpringBoot-related blogs is really mixed now. A considerable number of people still use it as SpringMvc; use it, but don’t use it. The new features are really meaningless.

The above is the detailed content of Detailed introduction of SpringBoot loading submodule configuration file (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete