Spring Boot
- 约定优于配置,种软件设计范式
一 基本应用
1.1 Spring优缺点分析
- 传统Spring的代码是轻量级的,但配置+依赖管理是重量级的。
1.2 Spring Boot解决上述spring问题
- 基于约定优于配置的思想,提供了起步依赖+自动配置。
1.3 Spring Boot入门
- 所有的springBoot项目都会直接或者间接的继承spring-boot-starter-parent。
- 编码UTF-8、JDK1.8。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
- 启动类
@SpringBootApplication
public class SpringBootDemo1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo1Application.class,args);
}
}
1.4 全局配置文件
- 优先级排序从低到高:yml>yaml>properties。
1.set方法注入实体
- 配置文件
person:
id: 18
name: 张三
hobby:
- 篮球
- 足球
family: [你,我,他]
map:
k1: v1
k2: v2
pet:
type: cat
name: haha
- 实体
@Component
@ConfigurationProperties(prefix ="person")
public class Person {
private int id; //id
private String name; //名称
private List hobby; //爱好
private String[] family; //家庭成员
private Map map;
private Pet pet; //宠物
}
- 上述代码使用@Component和@ConfigurationProperties(prefix = “person”)将配置文件中的每个属性 映射到person类组件中。
2. @Value注入属性
@Component
public class Pet {
@Value("${person.pet.type}")//map、对象、行内数据 集合类型不支持
private String type;
}
3. 自定义配置
@PropertySource("classpath:test.properties") // 指定自定义配置文件位置和名称
4. 自定义配置类
@Configuration // 定义该类是一个配置类````
public class MyConfig {
@Bean // 将返回值对象作为组件添加到Spring容器中,该组件id默认为方法名
public MyService myService(){
return new MyService();
}
}
1.5 SpringBoot原理深入
1.依赖管理
- 为什么导入dependency时不需要指定版本?
答:spring-boot-starter-parent依赖。
- 查看spring-boot-starter-parent底层源文件,发现spring-bootstarter-parent的底层有一个父依赖spring-boot-dependencies,核心代码具体如下
- 查看spring-boot-dependencies底层源文件,已经定义了版本号。
二 Spring Boot整合Redis
2.1 基础依赖
<!-- redis依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.2 编写配置文件
spring:
redis:
host: IP
port: 6379
password: ******
jedis:
pool:
max-active: 20 #连接池最大连接数
max-wait: 6000 #连接池最大阻塞等待时间
min-idle: 2 #连接池中的最小空闲连接
timeout: 5000 #连接超时时间(毫秒)
2.3 Redis工具类
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
/**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 写入缓存
*/
public boolean set( String key, Object value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value,1, TimeUnit.DAYS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 更新缓存
*/
public boolean getAndSet(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().getAndSet(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 删除缓存
*/
public boolean delete(final String key) {
boolean result = false;
try {
redisTemplate.delete(key);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
2.4 测试
@SpringBootTest
@RunWith(SpringRunner.class)
class Mybatis3ApplicationTests {
@Autowired
private RedisUtils redisUtils;
@Test
public void getRedisData() {
Comment comment = (Comment) redisUtils.get("comment");
System.out.println(comment);
}
}