springboot中的配置文件
使用yml或者properties配置
配置类名称是固定的:
- application.properties
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
spring.freemarker.suffix=.ftl
- application.yml
缩进一定不要用TAB,否则会报错,只能只用空格键!!!
spring:
freemarker:
allow-request-override: false
cache: true
check-template-location: true
charset: UTF-8
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
suffix: .ftl
可以分别配置多个测试环境方便修改
只需要修改第一个yml文件中active的名称。
如:
| - -application.yml
| - -application-dev.yml //本地环境
| - -application-prod.yml //生产环境
| - -application-test.yml //测试环境
application.yml写法:
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
application-dev.yml写法:(其他环境可以参照)
server:
#端口号
port: 8333
servlet:
contextPath: /xxx
spring:
#应用名
application:
name: xxxx-xxx-web
#上传文件大小
servlet:
multipart:
max-file-size: 100Mb
max-request-size: 100Mb
#数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xxxxxxx?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 1 from dual
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
#redis
redis:
host: 110.110.110.110
port: 6379
password:
timeout: 6000 # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
#日志
logging:
file: ../logs
level:
com.xxx: debug
#mybatis-plus
mybatis-plus:
mapper-locations: classpath*:mybatisplus/*.xml
typeAliasesPackage: com.xyj.**.model
configuration:
mapUnderscoreToCamelCase: true
file:
#文件仓库,保存上传文件
repository-path: /usr/local/nginx/html
使用配置类进行配置
在springboot项目中创建一个java类,使用注解@Configuration,则这个类是SpringBoot bean的创建的配置文件类。(可以命名config或configuration文件夹来保存多个配置文件)。SpringBoot java配置@Configuration 有两种写法
方法一:通过包扫描,将包下所有注解类,注入到spring容器中
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //1使用配置注解 ,表示这个类是配置文件
@ComponentScan("com.wisely.highlight_spring4.ch1.di") //2使用扫描注解
public class DiConfig {
}
方法二:不使用扫描 ,注解。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //1表示配置文件
public class JavaConfig {
@Bean //2spring调用这个方法直接把FunctionService这个类实例加入到spring容器中
public FunctionService functionService(){
return new FunctionService();
}
}
FunctionService也是没有使用注解
//1没有加Service注解
public class FunctionService {
public String sayHello(String word){
return "Hello " + word +" !";
}
}