使用STS软件创建SpringBoot项目:
然后完成。
访问链接:http://localhost:8080/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
目录结构:
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 {
以上则完成了配置文件里面的参数获取。
http://localhost:8888/HelloWorld/showJdbc
http://localhost:8888/HelloWorld/helloWorld