项目使用了Springboot(以下简称sb),然后按照规则,它是可以支持多配置文件的
然后我在项目中按照常规的配置方法,使用了一下三个配置文件:
application.properties
application-dev.properties
application-prod.propertis
重点问题如下:
在项目初始搭建阶段,只引用了sb的基本框架,使用了maven来管理的,application文件里面配置了spring.profiles.active=dev
可以正确的读取到里面的参数,如:默认端口为8080,我改为8888,是可以被使用的,
后来由于项目需要,我从pom文件里面添加了各种项目所需框架后,发现启动报错,由于线下环境的数据库地址和线上的环境是不同的,所以数据库地址,账号,密码的配置放在了dev(开发环境)和prod(线上环境)里,查看报错信息提示配置类里面的@Value(${spring.database.url})找不到dev.pro配置文件里面的spring.database.url参数,开发环境配置的端口为8888,此时端口号也改回了默认的8080,明显是dev文件无法读取,然后我将开发环境的配置参数都copy到了总配置文件,而总配置文件application.properties里面的参数是可以被正常读取的,数据库也正常连接上了,想问问有没有哪位大神遇到或者比较熟悉这种配置的能给指点一下
说明:在程序开发过程中可能会有这样的需求:开发和部署的配置信息可能会不同,以传统的方式就是在配置文件里面写好配置,在部署的时候再去修改这些配置,这样肯定会有很多问题,比如忘记修改、修改错误等。 而Springboot提供了多配置文件的支持解决了这一问题。
Springboot的多配置文件是指:系统中存在多个配置文件,在不同的运行环境使用不同的配置文件即可。
1、先找到application.properties该文件,这个文件是springBoot的配置文件基本入口
2、自定义一个文件:application-msg.properties
3、在application.properties中声明注册这个文件。
4、获取该文件中的值
注意导包:import org.springframework.beans.factory.annotation.Value;
5、测试
6、问题描述
1、@Value("${msg.content}") 该内容中的msg前缀是application-msg.properties 内容的前缀,spring.profiles.active=msg是指向程序去查找application-msg.properties该文件。
是不是maven没更新啊
把配置文件贴出来看看
这是pom文件
org.springframework.boot
spring-boot-starter-parent
1.5.7.RELEASE
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 下面两个引入为了操作数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Junit 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 为了监控数据库 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<!-- Json包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
下面是主配置文件
#spring.profiles.active=dev
spring.thymeleaf.prefix=classpath:thymeleaf/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
server.tomcat.access_log_enabled=true
server.tomcat.basedir=target/tomcat
multipart.maxFileSize=1000Mb
multipart.maxRequestSize=1000Mb
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://192.168.1.12:3306/zhaocaibao //本来这块是应该放到开发模式配置文件里的
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.useGlobalDataSourceStat=true
#JPA Configuration:
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
#spring.jpa.hibernate.ddl-auto=update
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
application.properties 文件中引入spring.profiles.active=dev即可