前面我们讲解了SpringBoot HelloWorld实现
今天具体来讲解上那个application.properties项目配置文件
打开是空白 里面可以配置项目,所以配置项目我们 alt+/ 都能提示出来

上一讲tomcat默认端口8080 默认路径是根目录/
我们现在改成 端口8080 以及上下文路径/HelloWorld
改完后保存,启动HelloWorldApplication类
页面输入:http://localhost:8888/HelloWorld/hello
结果出来了

SpringBoot支持自定义属性
我们在application.properties中加一个helloWorld属性,属性值spring Boot大爷你好
server.port=8888
server.context-path=/HelloWorld
helloWorld=spring Boot\u5927\u7237\u4F60\u597D
当然对中文字节编码处理了
package com.java1234;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Value("${helloWorld}")
private String helloWorld;
@RequestMapping("/hello")
public String say(){
return helloWorld;
}
}页面里 我们定义然后配置一个属性值,调用请求,可以直接返回配置的值。
这个是一个很好的功能,比较方便;
我们重启HelloWorldApplication类,
页面输入:http://localhost:8888/HelloWorld/hello
显示:

假如我们要配置一个类别下的多个属性,
比如mysql的jdbc连接配置
mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_diary
mysql.userName=root
mysql.password=123456
我们贴到application.properties
然后按照前面的方案,我们在Controller里写四个属性;
package com.java1234;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Value("${helloWorld}")
private String helloWorld;
@Value("${mysql.jdbcName}")
private String jdbcName;
@Value("${mysql.dbUrl}")
private String dbUrl;
@Value("${mysql.userName}")
private String userName;
@Value("${mysql.password}")
private String password;
@RequestMapping("/hello")
public String say(){
return helloWorld;
}
@RequestMapping("/showJdbc")
public String showJdbc(){
return "mysql.jdbcName:"+jdbcName+"<br/>"
+"mysql.dbUrl:"+dbUrl+"<br/>"
+"mysql.userName:"+userName+"<br/>"
+"mysql.password:"+password;
}
}重启启动类,
页面输入:http://localhost:8888/HelloWorld/showJdbc

上面那种 假如属性很多 要写一大串 假如多个地方使用 每个地方都得写这么多 不可取
下面我们介绍ConfigurationProperties配置方式
新建一个MysqlProperties类 把所有属性都配置上去
package com.java1234;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Mysql属性配置文件
* @author user
*
*/
@Component
@ConfigurationProperties(prefix="msyql")
public class MysqlProperties {
private String jdbcName;
private String dbUrl;
private String userName;
private String password;
public String getJdbcName() {
return jdbcName;
}
public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
}
public String getDbUrl() {
return dbUrl;
}
public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}执行前缀msyql
以及加上@Component作为组件 方便其他地方注入
当然这里会提示,

需要引入依赖到pom.xml
我们点下即可

pom.xml里会自动多了一个依赖,自动下载jar包
HelloWorldController里改下
package com.java1234;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Value("${helloWorld}")
private String helloWorld;
@Autowired
private MysqlProperties mysqlProperties;
@RequestMapping("/hello")
public String say(){
return helloWorld;
}
@RequestMapping("/showJdbc")
public String showJdbc(){
return "mysql.jdbcName:"+mysqlProperties.getJdbcName()+"<br/>"
+"mysql.dbUrl:"+mysqlProperties.getDbUrl()+"<br/>"
+"mysql.userName:"+mysqlProperties.getUserName()+"<br/>"
+"mysql.password:"+mysqlProperties.getPassword();
}
}只需要定义MysqlProperties即可 方便很多 运行就不演示了和前面一样;