多modules的项目读取启动模块的配置文件

现在我们的项目基本上都是由多个module组成的。但是不管有多少个module。启动模块都是只有一个。所以很多时候我们需要将一些可能在项目中需要被修改的文件放到启动模块的资源路劲中。因为不放在启动模块的资源路劲中,而放在本模块的resource下面,这样打成jar包是就会被打在jar包里面。这样就不利于项目现场进行修改。
下面是在其他模块读取启动模块的方法,仅以读取properties问价为例。
首先我们建立一个多module项目。如图:
在这里插入图片描述
接下来是去取方法:

第一种: 利用 **xxxxx.class.getClassLoader().getResourceAsStream(“xxx”)**来读取。
public class ModulesStaticFileReadTest{

    static {
        test1();
    }

    private static void test1() {
        InputStream ips = null;
        try {
            Properties props = new Properties();
            ips = ModulesStaticFileReadTest.class.getClassLoader().getResourceAsStream("info/info.properties");
            if (null != ips) {
                props.load(ips);
                Enumeration<String> enumeration = (Enumeration<String>) props.propertyNames();
                while (enumeration.hasMoreElements()) {
                    String key = enumeration.nextElement();
                    System.out.println(key + "-------" + props.get(key));
                }
            }
        } catch (Exception e) {
        } finally {
            try {
                if (null != ips) {
                    ips.close();
                }
            } catch (Exception e1) {
            }
        }
    }
}

测试结果如图:
在这里插入图片描述

第二种: 利用 注解读取读取。

用到的注解有:
@Configuration
@PropertySource(“classpath:info/info.properties”)
@Value("${xxx}")

完成参数的注入,一定要有@Configuration注解,不然不起作用

我们在firstmodule模块中建一个类用来接收 info.properties 中的配置项。

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Data
@Configuration
@PropertySource("classpath:info/info.properties")
public class InforProperties {
    @Value("${info.name}")
    String name;
    @Value("${info.age}")
    int age;
}

然后再读取里面的各个项:

/**
 * 测试多模块,其他模块读取,start启动模块下的resource配置文件 方法
 */
@Component
public class ModulesStaticFileReadTest implements CommandLineRunner {

    @Autowired
    private  InforProperties inforProperties;
    
    @Override
    public void run(String... args) throws Exception {
         System.out.println(inforProperties.getName() +"----" + inforProperties.getAge());
    }
}

启动整个项目,结果如图:
在这里插入图片描述
这样也能去读取到数据。

第三种: pom中实现资源共享。这是我看到其他博主写的。并没有测试过。这里附上原文链接

https://blog.csdn.net/mjlfto/article/details/89409685
数据库操作模块 : db --> 该模块中存在与数据相关的配置
web交互模块: web --> 该模块依赖db模块

这个时候如果不做共享处理, 那么在web中就需要与db中做相同的配置,显然麻烦并且不合理共享操作方式

在db pop.xml中配置

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
            <include>**/*.ftl</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>  

但是如上配置只是让maven在构建项目的时候将相应的配置文档放到的classes目录下,如果需要在依赖模块中application.properties中引用这些配置文件,
必须做相应的配置才可以

如:在依赖文件中做如下配置类,引入需要的配置文件,需要注意的时@Configuration注解必须存在,否则不生效

@Configuration
@PropertySource(value = "classpath:db.properties")
public class DbConfig {
}

那么这个时候就可以在依赖模块中使用db配置文件了

spring.profiles.include=db
spring.freemarker.suffix=.ftl

如有不当之处,欢迎指正,谢谢。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值