SpringBoot

1.运行原理

自动配置
①自动配置的东西都在springboot-autoconfigure的jar包中;
在这里插入图片描述
②自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件
在这里插入图片描述
有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;

在这里插入图片描述
④springboot的所有自动装配都是在启动时扫描并加载:所有的自动配置类都在spring.factories文件中,只要导入了对应的starter启动器就会生效
⑤一旦自动配置类生效,就会给容器添加各种组件

2.yaml

yaml

这种语言以数据作为中心,而不是以标记语言为重点!
①编写yaml配置

person:
  name: qinjiang
  age: 3
  happy: false
  birth: 2000/01/01
  maps: {
   k1: v1,k2: v2}
  lists:
   - code
   - girl
   - music
  dog:
    name: 旺财
    age: 1

②注入到我们的类中(set方法不可以省略)

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
   
    ...
}

@PropertySource:加载指定的配置文件;
@configurationProperties:默认从全局配置文件中获取值;

回顾properties配置

①编写user.properties

user1.name=kuangshen
user1.age=18
user1.sex=男

②在User类上使用@Value来进行注入

@Component //注册bean
@PropertySource(value = "classpath:user.properties")
public class User {
   
    //直接使用@value
    @Value("${user.name}") //从配置文件中取值
    private String name;
    @Value("#{9*2}")  // #{SPEL} Spring表达式
    private int age;
    @Value("男")  // 字面量
    private String sex;
}

数据校验

@validated

@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;

多环境切换

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本;如果yml和properties同时都配置了,默认使用.properties配置文件

①properties:
通过一个配置来选择需要激活的环境

spring.profiles.active=dev

②yaml:
使用yml去实现不需要创建多个配置文件,更加方便

server:
  port: 8081
#选择要激活那个环境块
spring:
  profiles:
    active: prod
---
server:
  port: 8083
spring:
  profiles: dev #配置环境的名称
---
server:
  port: 8084
spring:
  profiles: prod  #配置环境的名称

优先级

优先级1:项目路径下的config文件夹配置文件
优先级2:项目路径下配置文件
优先级3:资源路径下的config文件夹配置文件
优先级4:资源路径下配置文件

@Conditional

必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;
在这里插入图片描述

自动配置再理解

springboot一开始通过xxxAutoConfiguration帮我们自动装配配置类,一旦配置类生效,配置类添加的各种组件就会从xxxProperties获取默认值,修改默认值只需要按对应规则在配置文件中绑定配置

总结

1、SpringBoot启动会加载大量的自动配置类,通过源码分析可知从spring.factories文件中

2、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;

3、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件存在在其中,我们就不需要再手动配置了)

4、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;
可以在配置文件中启用 debug=true属性,知道哪些自动配置类生效;

3.静态资源处理

①以jar包的方式引入我们的静态资源
②导入自定义资源(如.js.html)
通过分析源码得出下面四个路径的静态资源可以被我们识别:

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

自定义静态资源路径:
在application.properties中配置:

spring.resources.static-locations=classpath:/coding/,classpath:/kuang/

配置后原来的自动配置就都会失效了

网站小图标

将图片放在与.html同目录下
在head中引入

<link rel="icon" type="image/x-icon" href="fav.ico"/>

4.扩展使用SpringMVC

我们要做的就是编写一个@Configuration注解类,
并且类型要为WebMvcConfigurer
还不能标注@EnableWebMvc注解,它代表我们全面接管了SpringMVC了,SpringBoot配置的静态资源映射会无效;
我们新建一个包叫config,写一个类MyMvcConfig

分析源码:
WebMvcAutoConfiguration-WebMvcAutoConfigurationAdapter上的注解EnableWebMvcConfiguration-父类DelegatingWebMvcConfiguration中的代码
结论:
所有的WebMvcConfiguration都会被作用,不止Spring自己的配置类,我们自己的配置类当然也会被调用

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
   

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
   
        // 浏览器发送/test , 就会跳转到test页面;
        registry.addViewController("/test").setViewName("test");
    }
}

5. 整合JDBC

新建一个springboot项目,引入相应的模块
在这里插入图片描述
编写yml配置文件连接数据库:

spring:
  datasource:
    username: root
    password: root
    #时区报错就增加时区配置
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver

配置完这一些东西后,我们就可以直接去使用了,因为SpringBoot已经默认帮我们进行了自动配置
查看源码:
DataSourceAutoConfiguration中,看出 Spring Boot 2.2.5 默认使用HikariDataSource 数据源,而以前版本,如 Spring Boot 1.5 默认使用 org.apache.tomcat.jdbc.pool.DataSource 作为数据源;
HikariDataSource 号称 Java WEB 当前速度最快的数据源

JDBCTemplate

Spring Boot 不仅提供了默认的数据源,同时默认已经配置好了 JdbcTemplate 放在了容器中,程序员只需自己注入即可使用

@Autowired
JdbcTemplate jdbcTemplate;

数据库操作的所有 CRUD 方法都在 JdbcTemplate 中。

update方法用于执行新增、修改、删除等语句
query方法及queryForXXX方法:用于执行查询相关语句

①增

String sql = "insert into user(id,name,pwd) values (5,'小明',123456)";
jdbcTemplate.update(sql);

②删

String sql = "delete from user where id=?";
jdbcTemplate.update(sql,id);

③改

String sql = "update user set name=?,pwd=? where id="+id;
jdbcTemplate.update(sql,objects);

④查

String sql="select * from user";
jdbcTemplate.queryForList(sql);

6.整合Druid

①导入Jar包

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.17</version>
</dependency>

②在配置文件通过spring.datasource.type 指定数据源。
③将自定义的 Druid数据源添加到容器中

@Configuration
public class DruidConfig {
   
//绑定配置文件
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
   
        return new DruidDataSource();
    }

④配置Druid数据源监控:完成后可访问:http://localhost:8080/druid/login.html

@Bean
    //后台监控:
    //内置 Servlet 容器时没有web.xml文件,所以使用 Spring Boot 的注册 Servlet方式:ServletRegistrationBean
    public ServletRegistrationBean stateViewServlet(){
   
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后台需要有人登录,账号密码配置
        HashMap<String,String> init = new HashMap<
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值