Home > Java > Java Tutorial > body text

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

不言
Release: 2019-02-16 13:47:48
forward
4827 people have browsed it

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
Copy after login

zero’s dependencies


    
      org.springframework.boot
      spring-boot-starter-web
    
  
Copy after login

ws’s dependencies and configuration


    
      cn.xmcui.zero
      service
      1.0-SNAPSHOT
    
    
      org.springframework.boot
      spring-boot-starter-web
    
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        
          
          cn.xmcui.zero.Application
          true
          ZIP
        
        
          
            
              repackage
            
          
        
      
    
  
Copy after login

ws’s application.yml

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

dao dependencies and configuration


    
      cn.xmcui.zero
      entity
      1.0-SNAPSHOT
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.3.2
    
    
      mysql
      mysql-connector-java
      5.1.47
    
  
Copy after login

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
Copy after login

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);
    }
}
Copy after login

Run

Then a welcome error

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

Description:

Failed to configure a DataSource: 'url' 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).
Copy after login

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
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!