spring boot遵循“约定优于配置”的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来。spring boot的神奇不是借助代码的生成来实现的,而是通过条件注解来实现的。
1.自定义配置,我们用yml配置我们自己的配置类:@ConfigurationProperties,
@ConfigurationProperties映射application.yml以test为前缀的配置,我就不详细介绍了。
可以参考:https://www.cnblogs.com/ginponson/p/6188432.html
在yml中的配置
指定端口:
指定默认启动环境
mybatis配置
mybatis在启动时可能会出现找不到的问题,我们把xml文件放到resource下的mapper中:如下
然后就是你在开发环境和测试环境想相互切换的配置
你把dev替换为test就ok了。
这里就有两种了:
1.一个是没有打包之前,我用的STS开发工具,我只想调试一个模块,而其他的模块在测试环境中,你可能没有其他模块的jar包,又不可能在本地运行,那我们可以直接在你启动的项目去切换你的环境。找到你的项目右键点击,找到OpenConfig如下:
找到Profile,将你在application.yml配置的环境填入就ok,然后启动。
2.你已经打包了,然后我想切换环境,总不可能在去把application.yml配置的环境修改在去打包吧,然后你在启动jar包时,修改你的环境,如下:
java -jar test-service.jar --spring.profiles.active=test
下面是所有代码:
package com.test.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.redis.model.TestModel;
@ConfigurationProperties(prefix = "test")
public class TestConfig{
private String name;
private String password;
private TestModel testmodel;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public TestModel getTestmodel() {
return testmodel;
}
public void setTestmodel(TestModel testmodel) {
this.testmodel = testmodel;
}
}
package com.test.model;
public class TestModel {
private Long id;
private String hostPort;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getHostPort() {
return hostPort;
}
public void setHostPort(String hostPort) {
this.hostPort = hostPort;
}
}
application.yml配置
# 默认的profile为dev,其他环境通过指定启动参数使用不同的profile,比如:
# 测试环境:java -jar test-service.jar --spring.profiles.active=test
# 生产环境:java -jar test-service.jar --spring.profiles.active=prod
server:
port: 9005 #指定启动端口号
spring:
application:
name: test-service
profiles:
active: dev #默认环境(开发环境)
datasource:
type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息
url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
mybatis:
mapper-locations: classpath*:/mapper/**Mapper.xml #把xml文件放在com.XX.mapper.*中可能会出现找不到的问题,这里把他放在resource下的mapper中
typeAliasesPackage: com.test.domain #这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
logging:
file: test-service.log
level:
com.test: debug
#自己定义的配置
test:
name:
password:
testmodel:
id: 1
host-port: 127.0.0.1:8080
--- #注意前面有短横线
#测试环境###########################################################################################################################################
spring:
application:
name: test-service
profiles: test #测试环境
datasource:
type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息
url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
mybatis:
mapper-locations: classpath*:/mapper/**Mapper.xml #把xml文件放在com.XX.mapper.*中可能会出现找到的问题,这里把他放在resource下的mapper中
typeAliasesPackage: com.test.domain #这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
logging:
file: test-service.log
level:
com.test: debug
--- #注意前面的短横线
#正式环境###########################################################################################################################################
spring:
application:
name: test-service
profiles: prod
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: test
password: test
mybatis:
mapper-locations: classpath*:/mapper/**Mapper.xml
typeAliasesPackage: com.test.domain
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
logging:
file: test-service.log
level:
com.test: debug
SpringBoot