springboot加载子模块配置文件

在springboot项目搭建中,使用模块化构建的话,难免碰到要读取其他模块中的配置文件的情况。此时,springboot默认情况下只会读取启动类模块的配置文件。大概情况如图所示:

我的主配置类:

server.port=8080

我的其他模块配置类:

server.port=8090
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.password=root
spring.datasource.username=root
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.show-sql= true
spring.jpa.database = MYSQL

要想所有配置文件都加载的话,可以在启动类上添加@PropertySource标签,在期中配置value数组即可

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource(value={"classpath:application.properties","application_sub_demo.properties"})
public class DemoApplication extends SpringBootServletInitializer {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }

}

 

springboot正常启动,启动端口为8080

亲测可用。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一种非常流行的Java开发框架,它提供了一种方便的方式来管理应用程序的配置文件。在Spring Boot应用程序中,通常会使用application.properties或者application.yml来存储配置信息。但是,在复杂的应用中,可能存在多个模块,每个模块都需要自己的配置文件。在这种情况下,如何跨模块读取配置文件? 首先,需要明确的是,在Spring Boot中,配置文件读取是通过Spring的Environment接口来实现的。我们可以通过Environment对象来获取配置文件中定义的属性。因此,如果要跨模块读取配置文件,就需要在不同的模块中都能够访问到Environment对象。 解决这个问题的一个简单方法是,使用Spring Boot提供的@PropertySource注解。这个注解可以将指定的配置文件加载到Environment对象中。因此,我们只需要在每个模块中都添加一个与其他模块不同的配置文件,并在每个模块中使用@PropertySource注解来加载自己的配置文件,就可以实现跨模块读取配置文件的目的。 另外,Spring Boot还提供了一种更加高级的方式来管理配置文件,即使用Spring Cloud Config服务器。通过使用这种方式,在不同的模块中都可以通过统一的Config服务器来读取配置信息,避免了多个配置文件的冗杂。不过,这种方式需要额外的配置工作,适用于更加复杂的应用场景。 综上所述,跨模块读取配置文件可以通过在每个模块中使用@PropertySource注解来实现。同时,也可以考虑使用Spring Cloud Config服务器来管理配置信息,以提高整个应用的可维护性和可扩展性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值