SpringBoot01(helloWorld、获取配置文件的值)

使用STS软件创建SpringBoot项目:

SpringBoot01(helloWorld、获取配置文件的值)


然后完成。

SpringBoot01(helloWorld、获取配置文件的值)


访问链接:http://localhost:8080/helloWorld

SpringBoot01(helloWorld、获取配置文件的值)

基础的SpringBoot访问则完成。




配置获取配置文件里面配置的参数信息。

application.properties:


server.port=8888
server.context-path=/HelloWorld



helloWorld=spring boot Hello World


mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/springbootdb
mysql.userName=root
mysql.password=123456



目录结构:

SpringBoot01(helloWorld、获取配置文件的值)


MysqlProperties用来接收配置文件里面的参数数据:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Mysql属性配置文件
 * @author Administrator
 *
 */
@Component
@ConfigurationProperties(prefix="mysql")
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;
}

}



HelloWorldController控制器:

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.java1234.properties.MysqlProperties;

@RestController

public class HelloWorldController {


@Value("${helloWorld}")
private String helloWorld;

@Resource
private MysqlProperties mysqlProperties;

@RequestMapping("/helloWorld")
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()+"<br/>";
}
}



以上则完成了配置文件里面的参数获取。

http://localhost:8888/HelloWorld/showJdbc

SpringBoot01(helloWorld、获取配置文件的值)

http://localhost:8888/HelloWorld/helloWorld

SpringBoot01(helloWorld、获取配置文件的值)