共计 2392 个字符,预计需要花费 6 分钟才能阅读完成。
1 day01
1.1 @SpringBootApplication 启动类
@Target({ElementType.TYPE}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Documented | |
@Inherited | |
@SpringBootConfiguration | |
@EnableAutoConfiguration | |
@ComponentScan |
1.2 SpringMVC 的的过程
1.3 Pom.xml 的了解
2 day02
2.1 配置文件赋值操作
1. 入门获取 IP 和端口号
package com.jt.controller; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.servlet.ModelAndView; | |
//@Controller //String 类型 /moduleAndView | |
public class RedisController { | |
private String host = "127.0.0.1"; | |
private Integer port = 6379; | |
// 如果应用 RestController 返回值为 String 类型则返回字符串自身 | |
// 如果返回的是一个对象 则后果必然是该对象的 JSON 数据. | |
@RequestMapping("/getMsg") | |
public String getMsg(){return host + ":" + port;} | |
} |
- @Value 注解属性赋值
package com.jt.controller; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.servlet.ModelAndView; | |
@RestController //@ResponseBody 将返回值转化为 json 串应用 程序将不会执行视图解析器 间接返回 | |
//@Controller //String 类型 /moduleAndView | |
public class RedisController { | |
/** | |
* 实现思路: | |
* 如果能够从容器中获取数据的化, 间接赋值给属性. 则能够实现解耦 | |
* 如何实现: | |
* 注解实现: @Value("${ 配置文件的 key}") | |
* 表达式: spel 表达式 | |
*/ | |
@Value("${redis.host}") | |
private String host; | |
@Value("${redis.port}") | |
private Integer port; | |
// 如果应用 RestController 返回值为 String 类型则返回字符串自身 | |
// 如果返回的是一个对象 则后果必然是该对象的 JSON 数据. | |
@RequestMapping("/getMsg") | |
public String getMsg(){return host + ":" + port;} | |
} |
3. 利用 properties 文件为属性赋值(配置文件 +controller)
redis.properties
redis.pro.host=redis 的配置文件 | |
redis.pro.port=6379 |
controller
@RestController | |
// 须要通过 spring 容器加载配置文件, 并且以 utf- 8 的格局进行加载 | |
@PropertySource(value="classpath:/properties/redis.properties",encoding = "UTF-8") | |
public class RedisProController {@Value("${redis.pro.host}") | |
private String proHost; | |
@Value("${redis.pro.port}") | |
private Integer proPort; | |
@RequestMapping("/getMsgPro") | |
public String getMsg2(){return proHost + ":" + proPort;} | |
} |
4.SpringBoot 环境切换问题(YML+controller)
# 该配置文件, 当 spring 容器启动时加载. | |
spring: | |
profiles: | |
active: prod | |
--- | |
# 定义开发环境 | |
spring: | |
profiles: dev | |
server: | |
port: 8080 | |
#配置 redis 节点信息 | |
redis: | |
host: 192.168.1.100 | |
port: 6379 | |
# 如果须要多环境配置则须要将 YML 环境宰割 | |
--- | |
spring: | |
profiles: prod | |
server: | |
port: 8090 | |
#配置 redis 节点信息 | |
redis: | |
host: 10.0.0.1 | |
port: 6379 |
正文完