SpringBoot笔记(狂神说)

本文详细介绍了SpringBoot的基础知识,包括自动配置原理、启动器和启动类的作用。深入探讨了Swagger的使用,Spring Security的身份验证和权限控制,以及Shiro的安全框架。同时,讲解了Dubbo+Zookeeper的RPC实现,以及Redis在SpringBoot中的应用。内容涵盖从基础到高级的多个方面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot部分知识

基础

自动配置原理

pom.xml:

  • spring-boot-dependencies:核心依赖在父工程中
  • 引入SpringBoot依赖时,不需要指定版本,就是因为有这些版本仓库

启动器:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 就是SpringBoot的启动场景
  • 会自动帮我们导入该模块的所有依赖

启动类:

  • @SpringBootApplication:标注这个类是一个SpringBoot应用
两个核心注解:
@SpringBootConfiguration:SpringBoot的配置
	@Configuration:Spring配置类
		@Component:说明这也是一个Spring的组件
@EnableAutoConfiguration:自动配置
	@AutoConfigurationPackage:自动导入包
		@Import(AutoConfigurationPackages.Registrar.class):导入选择器
			metadata:源数据
     @Import(EnableAutoConfigurationImportSelector.class):自动配置导入选择
     	AutoConfigurationImportSelector:
     				List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);//获取所有的配置
	protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
			AnnotationAttributes attributes) {
   
		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
				getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
		Assert.notEmpty(configurations,
				"No auto configuration classes found in META-INF/spring.factories. If you "
						+ "are using a custom packaging, make sure that file is correct.");
		return configurations;
	}

​ META-INF/spring.factories:自动配置的核心

在这里插入图片描述

@ConfigurationProperties

/*
@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
*/
@Component //注册bean  必须要添加这个注解
@ConfigurationProperties(prefix = "person")
public class Person {
   
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

JSR303数据校验

--示例
@Validated  //数据校验
public class Person {
   

    @Email(message="邮箱格式错误") //email必须是邮箱格式,message是报错信息
    private String email;
}
--常见参数
@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;

空检查
@Null       验证对象是否为null
@NotNull    验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank   检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty   检查约束元素是否为NULL或者是EMPTY.
    
Booelan检查
@AssertTrue     验证 Boolean 对象是否为 true  
@AssertFalse    验证 Boolean 对象是否为 false  
    
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内  
@Length(min=, max=) string is between min and max included.

日期检查
@Past       验证 Date 和 Calendar 对象是否在当前时间之前  
@Future     验证 Date 和 Calendar 对象是否在当前时间之后  
@Pattern    验证 String 对象是否符合正则表达式的规则

邮件任务

  1. 导入依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
  1. 在邮箱中开启POP3/SMTP服务

    将授权码作为邮箱密码

  2. 配置邮箱

  mail:
    username: 1558647481@qq.com
    password: hnbikvdksulfjeih  #可以是授权码 也可以是密码
    host: smtp.qq.com
    #开启加密授权验证
    properties:
      mail.smtp.auth: true
  1. 测试
    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {
   

        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("1558647481@qq.com");
        mailMessage.setTo("1558647481@qq.com");
        mailMessage.setSubject("主题");
        mailMessage.setText("正文");
        mailSender.send(mailMessage);

    }

    @Test
    void  test() throws MessagingException {
   

        //一个复杂的邮件
        MimeMessage message  = mailSender.createMimeMessage();
        //多文件
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setSubject("主题");
        helper.setText("<p>网页</p>", true); //支持网页

        File file1 = new File("/Users/reggie/ttc/C-销售/个人汇总.xlsx");
        File file2 = new File("/Users/reggie/ttc/C-销售/明细.xlsx");
        //附件
        helper.addAttachment("汇总.xlsx", file1);
        helper.addAttachment("明细.xlsx", file2);

        mailSender.send(message);
    }

定时任务

TaskScheduler  任务调度者
TaskExecutor	任务执行者

@EnableScheduling	//开启定时功能的注解(加在启动类上)
@Sheduled	//什么时候执行
@Service
public class TaskService {
   
    //秒   分   时     日   月   周几
    @Scheduled(cron = "0 48 9 *  * ?")
    public void hello(){
   
        System.out.println("hello");
    }
}

异步任务

使用异步任务的好处:

​ 我们如果想让用户快速得到反馈,就在后台使用多线程的方式进行处理即可,但是每次都需要自己手动去编写多线程的实现的话,太麻烦了,我们只需要用一个简单的办法,在我们的方法上加一个简单的注解即可,如下:

@EnableAsync //开启异步功能 加在启动类上
@Async  //加在方法上 异步方法
@Service
public class AsyncService {
   
    //告诉Spring这是一个异步方法
    @Async
    public void hello(){
   
        try {
   
            Thread.sleep(3000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        System.out.println("执行完毕");
    }
}

SpringBoot就会自己开一个线程池,进行调用!

Swagger

  1. 导入依赖
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2<
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值