SpringBoot

1.@SpringBootApplication注解是Spring Boot的核心注解

2.springboot的包应该建在和启动类同级的目录下

3.原理

3.1pom文件

Spring Boot Dependencies:核心依赖在父工程中

3.2启动器

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.2.0.RELEASE</version>
    </dependency>

3.3主程序

@SpringBootApplication//@SpringBootApplication标注这个类是springboot的一个应用
public class SpringbootApplication {
    public static void main(String[] args) {
        //将springboot应用启动
        SpringApplication.run(SpringbootApplication.class, args);
    }

}

3.4注解

@SpringBootConfiguration :springboot的配置

@Configuration:spring的配置类

        @Component:spring的组件

@EnableAutoConfiguration:自动配置

@AutoConfigurationPackage:自动配置包

      @Import({Registrar.class}):自动配置`包注册`

   @Import({AutoConfigurationImportSelector.class}):自动配置导入选择
//获取所有的配置
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);

获取候选的配置

 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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:自动配置的核心文件

4.yml语法

添加注解:

<dependency>
        <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.4.3</version>
       </dependency>

5.JSR303数据校验

@validated注解

6.多环境配置和配置文件位置

配置文件位置的优先级,由高到低:

多环境配置:

server:
  port: 8081
spring:
  profiles:
    active: dev
---
server:
  port: 8082

spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: test

7.springboo tweb开发

7.1静态资源

  public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }

webjars:可以通过静态资源的jar包导入静态资源,添加maven依赖即可

总结:

1.在springboot中,可以使用一下方式获取静态资源

  • webjars  localhost:8080/webjars/
    • public、static、/**、recources      localhost:8080/

2.优先级  recources>static>public

7.2thymeleaf模板引擎

    添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

语法:

1.th:text与th:utext与th:each

@Controller
public class HelloController {
    @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("msg","<h1>hello world</h1>");
        model.addAttribute("testList", Arrays.asList("a1","a2"));
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--未转译-->
<div th:text="${msg}"></div>
<!--转译-->
<div th:utext="${msg}"></div>
<!--遍历方式一-->
<h1 th:each="a:${testList}">[[${a}]]</h1>
<!--遍历方式二-->
<!--<h1 th:each="a:${testList}" th:text="${a}"></h1>-->
</body>
</html>

输出结果:

7.3项目的国际化,可以切换不同的语言

  注意:首先idea的file encodings的配置

  •  需要配置i18n文件
  • 如果需要在项目中进行按钮自动切换,我们需要定义一个组件
  • 记得将组件放在spring容器中

8.整合JDBC

      # 添加依赖
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
         </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

9.整合Druid数据源

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.5</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

10.整合mybatis

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

11.SpringSecurity(安全)

功能:认证,授权

权限:功能权限

          访问权限

          菜单权限

拦截器,过滤器,大量的原生代码

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值