spring boot开始篇

一、编写第一个REST接口:

/**
* Spring Boot HelloWorld案例
*
* Created by bysocket on 16/4/26.
*/
@RestController
public class HelloWorldController {
@Autowired
Environment env;

@Value("${server.port}")
private String portt;

@Autowired
private MyConfiguration configuration;

@RequestMapping("/hello")
public String sayHello() {

return "Hello,World!" + configuration.getName();
}
}
@RestControlller是@Controller和@ResponseBody的组合注解,可以直接返回Json格式数据。@GetMapping是@RequestMapping(method=RequestMethod.GET).通过localhost:8080/hello即可以访问。
二、配置文件的读取:
1、Evnironment:
//注入方式
@Autowired
private Environment env;

@GetMapping("/getPort")
public String getPort(){
  return env.getProperty("server.port");
}

2. value注入方式
@Value("${server.port}")
private String port;

3.自定义配置方式
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "com.zy.firstboot")
@Component
public class MyConfiguration {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

三、profiles多环境配置:
application.properties 通用配置文件
-dev:开发环境
-test:测试环境
-prod:生产环境

四、actuator监控
加入引用:spring-boot-starter-actuator

猜你喜欢

转载自www.cnblogs.com/zhouyuekji/p/12409130.html