spring boot通过maven filter替换properties属性(多环境配置)

3 篇文章 0 订阅
1 篇文章 0 订阅

这两天项目到了差不多收尾了,想把各种环境配置通过maven filter在maven打包的时候就替换掉properties的配置。之前一直用${my.properies}的方式,打包了以后就是替换不掉properties里的属性。
这是我的maven filter的属性

filter.spring.freemarker.template-loader-path=file:/mnt/web/ftl/
filter.spring.resources.static-locations=file:/mnt/web/static/
filter.server.port=80
#mysql connection info
filter.jdbc.url=jdbc:mysql://192.168.1.111:3306/ubip?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true
filter.jdbc.username=root
filter.jdbc.password=banger

#redis config
filter.redis.database=0
filter.redis.host=192.168.1.111
filter.redis.port=6379
filter.redis.password=1234

在application.properties文件里需要被替换的属性

jdbc.url=${filter.jdbc.url}
...

maven配置

    <profiles>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>
    <build>
        <filters> <!-- 指定使用的 filter -->
            <filter>src/main/filters/${env}.properties</filter>
        </filters>
        <resources>
            <resource> <!-- 配置需要被替换的资源文件路径, properties 应该在 src/main/resource 目录下 -->
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>static/**</exclude>
                    <exclude>ftl/**</exclude>
                </excludes>
                <filtering>true</filtering> <!-- 是否使用过滤器 -->
            </resource>
        </resources>
    </build>

以前这样配置都是可以的。在spring boot里就不行。后来看了官方文档,里面有这样一句话

13.2 Maven
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:

Java 1.6 as the default compiler level.
UTF-8 source encoding.
A Dependency Management section, allowing you to omit tags for common dependencies, inherited from the spring-boot-dependencies POM.
Sensible resource filtering.
Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).
Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties and application-foo.yml)
On the last point: since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).

大致的意思是我的maven继承了spring-boot-starter-parent,并且spring默认配置文件接受的占位符也是 ...mavenfilter {}占位符就被spring的maven pom替换掉了,变成了@..@,我们可以通过resource.delimiter来覆盖它。
看下spring-boot-starter-parent 这个pom里写这一段

<properties>
        <java.version>1.6</java.version>
        <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

那一切就清楚了,我将properties中的占位符替换下变成

#mysql connection info
jdbc.url=@filter.jdbc.url@
jdbc.username=@filter.jdbc.username@
jdbc.password=@filter.jdbc.password@

#redis config
# Redis数据库索引(默认为0)
redis.database=@filter.redis.database@
# Redis服务器地址
redis.host=@filter.redis.host@
# Redis服务器连接端口
redis.port=@filter.redis.port@
# Redis服务器连接密码(默认为空)
redis.password=@filter.redis.password@

这样maven就能替换属性了。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要使用 `druid-spring-boot-starter`,你需要在你的 Maven 项目中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> ``` 然后,你可以在你的 `application.properties` 或 `application.yml` 文件中配置 `druid` 数据源。以下是一个示例: ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC username: root password: yourpassword type: com.alibaba.druid.pool.DruidDataSource # 下面是 druid 配置 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 initial-size: 5 # 最小连接池数量 min-idle: 5 # 最大连接池数量 max-active: 20 # 获取连接时最大等待时间,单位毫秒 max-wait: 60000 # 是否开启 PSCache pool-prepared-statements: true # 指定每个连接上 PSCache 的大小 max-pool-prepared-statement-per-connection-size: 20 # 打开removeAbandoned功能 remove-abandoned: true # 180秒,也就是3分钟 remove-abandoned-timeout: 180 # 关闭abanded连接时输出错误日志 log-abandoned: true # 监控配置 filter: # 开启监控统计功能 stat: enabled: true # 是否打印 SQL 语句 log-slow-sql: true # 慢 SQL 记录时间阈值,单位毫秒 slow-sql-millis: 5000 # 配置监控统计拦截的 URI,多个用逗号隔开 web-stat-filter: exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" # 配置 Druid 的 StatViewServlet stat-view-servlet: url-pattern: /druid/* # IP 白名单 allow: 127.0.0.1 # IP 黑名单(共同存在时,deny优先于allow) deny: 192.168.0.1 # 登录用户名 login-username: admin # 登录密码 login-password: admin123 ``` 以上是一个基本的 `druid` 配置,你可以根据自己的实际需求进行调整。配置完成后,你就可以在代码中使用 `DataSource` 了,例如: ```java @Autowired DataSource dataSource; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值