springboot配置

匿名 (未验证) 提交于 2019-12-03 00:17:01

1、自定义静态资源访问

第一种方式

1、配置类

@Configuration

publicclassimplements

publicvoid

springboot\\pic\\ 访问都映射到/myPic/** 路径下

}

/myPic/**

2

例如,在D:/springboot/pic/logo.jpg图片

http://localhost:8080/myPic/logo.jpg

第二种方式

application.properties

web.upload-path=D:/springboot/pic/

spring.mvc.static-path-pattern=/**

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,\

注意:

web.upload-path/

spring.mvc.static-path-pattern=/**

spring.resources.static-locationsstaticpublicfile:${web.upload-path}file:classpath

然后,重启项目

例如,在D:/springboot/pic/8.png图片

http://localhost:8080/8.png

2、自定义属性及读取

application.ymlSpring@Value(${})

1application.yml

offcn_ip:

offcn_port:

(2)Controller

@RestController

publicclass

@Value("${offcn_ip}")

private

@Value("${offcn_port}")

private

@GetMapping("/getvalue")

public

return

}

}

http://localhost:8888/java001/getvalue

2、实体类属性赋值

当属性参数变多的时候,我们习惯创建一个实体,用实体来统一接收赋值这些属性。

(1)、定义配置文件

userbody:

(2)、创建实体类

@ConfigurationProperties(prefix="userbody")

publicclass

private

private

private

private

private

}

提供setter、getter、以及toString

@ConfigurationPropertiesprrfix

(3)Controllerbean

@RestController

@EnableConfigurationProperties({UserBody.class})

publicclass

@Autowired

public

return

EnableConfigurationPropertiesSpringbootSimpleApplication

访问地址:http://localhost:8888/java001/getUser

1、多环境配置文件

yml

ymlapplication.ymlapplication-{profile}.ymlapplication-test.yml application-prod.yml

application.ymlspring.profiles.active=xxx,application.ymlprofiles

(1)application-dev.yml

server:

(2)application-test.yml

server:

(3)application-prod.yml

server:

(4)application.yml

spring:

active: {profile}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!