惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Jina AI
Jina AI
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
博客园 - 司徒正美
罗磊的独立博客
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Vercel News
Vercel News
A
About on SuperTechFans
I
InfoQ
D
DataBreaches.Net
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队

博客园 - 飘飘雪

Redis 的 Rehash 操作详解 Panic 与 Crash 的区别 vscode常用快捷键大全 网络流量单位 Core文件作用、设置及用法 linux批量执行工具omnitty安装及使用 linux 下误改了/etc/profile下的文件path路径导致整个系统用不了命令 python安装 linux less和more命令用法 Tair中对热key的内部处理 Tair中对大key的内部处理方式 redis的redis-benchmark用法 redis的memtier-benchmark用法 阿里巴巴的缓存类测试产品及有缺点 profiles下的properties与properties有什么区别 Guava 工具类之 Splitter的使用 什么是“黑天鹅”和“灰犀牛” java-sdk接口测试覆盖率统计实践 java应用接口自动化覆盖率统计实践
如何在class文件中使用pom中profile级别的 <properties>
飘飘雪 · 2024-09-03 · via 博客园 - 飘飘雪

在 Java 代码中使用 pom.xml 中 profile 级别的 <properties>,最常见和推荐的方式是通过 Maven Resource Filtering。这个过程涉及到以下步骤:

  1. 在 pom.xml 中定义 profile 和 properties:

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <db.url>jdbc:mysql://localhost:3306/mydb_dev</db.url>
                <server.address>localhost</server.address>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <db.url>jdbc:mysql://prod-db:3306/mydb_prod</db.url>
                <server.address>prod-server</server.address>
            </properties>
        </profile>
    </profiles>
  2. 在 src/main/resources 中创建一个.properties 文件:

    database.url=${db.url}
    server.address=${server.address}
  3. 在 pom.xml 中配置资源过滤:

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
  4. 在 Java 代码中加载和使用属性:

    • 使用 Properties 类:

      import java.io.InputStream;
      import java.util.Properties;
      
      //...
      
      Properties props = new Properties();
      InputStream inputStream = getClass().getClassLoader().getResourceAsStream("application.properties");
      props.load(inputStream);
      
      String databaseUrl = props.getProperty("database.url");
      String serverAddress = props.getProperty("server.address");
    • 使用 Spring 的 @Value 注解:

      import org.springframework.beans.factory.annotation.Value;
      
      //...
      
      @Value("${database.url}")
      private String databaseUrl;
      
      @Value("${server.address}")
      private String serverAddress;
    • 当你在开发环境中运行 Maven 构建时,dev profile 是默认激活的,所以 application.properties 文件中会被替换为 dev profile 中的属性值。如果你想在构建时使用 prod profile,可以在命令行中添加 -Pprod 参数,例如:mvn package -Pprod

这样,application.properties 文件中就会被替换为 prod profile 中的属性值。无论你在哪个环境中构建和运行应用,Java 代码中使用的属性值都会根据当前激活的 profile 自动更新。