美文网首页
通过maven向springboot项目注入配置的坑

通过maven向springboot项目注入配置的坑

作者: 翔哥不哭 | 来源:发表于2019-01-18 17:02 被阅读0次

配置文件

server.port=8888
spring.profiles.active=@env@
spring.datasource.url =@spring.datasource.url@
spring.datasource.username =@spring.datasource.username@
spring.datasource.password =@spring.datasource.password@
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
  • 下面是spring-boot-starter-parent包的pom,里面就有说到要用@
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <packaging>pom</packaging>
    <name>Spring Boot Starter Parent</name>
                    ......
    <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>
  • 启动类可以打印出maven启动环境是否正确
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(TestApplication.class, args);
        String[] activeProfiles = ctx.getEnvironment().getActiveProfiles();
        for (String profile : activeProfiles) {
            System.out.println("Spring Boot 使用profile为:" +  profile);
        }
    }
}
  • maven的配置
<profiles>
        <!-- 开发环境,默认激活 -->
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
                <maven.javadoc.skip>true</maven.javadoc.skip>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
                <maven.test.skip>true</maven.test.skip>
                <maven.javadoc.skip>true</maven.javadoc.skip>
            </properties>
        </profile>
    </profiles>
---------------------------------以下 放在<build>标签类

        <defaultGoal>install</defaultGoal>
        <filters>
            <filter>../env/filter-${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>application.properties</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>

启动命令 spring-boot:run -P dev -e ,-P后面的dev要跟<filters>标签内引用的${env}能匹配到正确名称的配置文件

  • 外面的配置文件filter-${env}.properties
## 服务端口 ## start ###################################
server.port=8888
## 服务端口 end #####################################

## oracle ## start ###################################
spring.datasource.url =XXX
spring.datasource.username =XXX
spring.datasource.password =XXX  
## oracle ## end #####################################

相关文章

网友评论

      本文标题:通过maven向springboot项目注入配置的坑

      本文链接:https://www.haomeiwen.com/subject/avgndqtx.html