springBoot

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依赖。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5BqAroLo-1622702839414)(C:\Users\HUSKY\AppData\Roaming\Typora\typora-user-images\image-20210603143528398.png)]

  • 查看spring-boot-starter-parent底层源文件,发现spring-bootstarter-parent的底层有一个父依赖spring-boot-dependencies,核心代码具体如下

在这里插入图片描述

  • 查看spring-boot-dependencies底层源文件,已经定义了版本号。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z0SHGe3D-1622702839426)(C:\Users\HUSKY\AppData\Roaming\Typora\typora-user-images\image-20210603143734524.png)]

二 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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值