springboot的基本使用(一)

1.springboot项目的简单搭建

http://blog.****.net/qq_37730579/article/details/79065388点击打开链接

2.springboot的配置文件

 springboot使用一个全局配置文件 文件名为 application.properties 或者是application.yml 放置在 src/main/resources目录或者类路径的/config下

 springboot全局配置文件的作用:对默认配置进行修改 如:对Tomcat端口的修改 server.port=8081 等

全局配置信息:

http://blog.****.net/qq_37730579/article/details/79066049点击打开链接

3.配置文件的读取

将配置文件放在 src/main/resources的resource目录中 

springboot的基本使用(一)

user.properties的文件内容为:

springboot的基本使用(一)

配置文件类:

springboot的基本使用(一)

读取配置文件的内容:

@Value("${user.username}")
private String username;

或者使用类型安全的配置 :

将user.properties 中的内容封装为一个实体对象 如:User类

springboot的基本使用(一)

读取方式:

springboot的基本使用(一)

乱码问题的解决:

在application.properties中配置

banner.charset=UTF-8
 server.tomcat.uri-encoding=UTF-8
 spring.http.encoding.charset=UTF-8
 spring.http.encoding.enabled=true
 spring.http.encoding.force=true
 spring.messages.encoding=UTF-8

如果还是不能解决问题 还需要在eclipse中配置:

http://www.cnblogs.com/zdz8207/p/java-eclipse-properties.html点击打开链接

4.Profile配置

Profile是spring用来针对不同的配置提供支持,全局Profile配置使用application-(profile).properties (application-prod.properties)

通过在application.properties中设置spring.profiles.active = prod 类指定活动的Profile

5.eclipse中VM arguments 的配置和读取

配置:

springboot的基本使用(一)

读取:

System.getProperty("server.port")