IDEA快捷键:
Alt + Ins
:生成getter,setter、构造器等代码。Ctrl + Alt + B
:查看类的具体实现代码。ctrl + shift + alt + U
:以图的方式显示项目中依赖之间的关系。alt + ins
:相当于Eclipse的 Ctrl + N,创建新类,新包等。- Ctrl + Alt + U : 以UML的类图展现类有哪些继承类,派生类以及实现哪些接口。
- Crtl + Alt + Shift + U : 同上,区别在于上条快捷键结果在新页展现,而本条快捷键结果在弹窗展现。
- Ctrl + H : 以树形方式展现类层次结构图。
spring入门
spirngboot优点
- 独立创建spring应用
- 内嵌web服务器
- 整合配置文件,自动整合Spring以及第三方功能,无需代码生成,无需写xml
- 能够健康外部检查,外部配置优化(可以在外部改写)
springboot项目建立
https://www.yuque.com/atguigu/springboot/lcfeme
第一:创建mavan
第二:引入依赖,pom.xml
//依赖管理
//父依赖声明依赖的版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
第三:创建主程序
@SpringBootApplication
public class mainapplication {
public static void main(String[] args) {
SpringApplication.run(mainapplication.class,args);
}
}
第四:编写业务
@RestController
//
//@Controller
//@ResponseBody
public class Hellotest {
@RequestMapping("/hello")
public String handle01()
{
return "hello springboot test1";
}
}
第五:添加配置(简化)简化部署
建立application.properties
把项目打成jar包,直接在目标服务器执行即可
springboot的配置,依赖,注解
-
开发导入starter场景启动器,见到很多 spring-boot-starter-* : *就某种场景,只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
-
无需关注版本号,自动版本仲裁
-
可以修改默认版本号
底层注解
底层注解-组件依赖-(@Configuration)
定义:一个组件中调用了另一个组件。proxyBeanMethods = true
- 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
- 2、配置类本身也是组件
- 3、proxyBeanMethods:代理bean的方法
- Full(proxyBeanMethods = true)(保证每个@Bean方法被调用多少次返回的组件都是单实例的,从容器中获取,此为代理配置)(默认)
- Lite(proxyBeanMethods = false)(每个@Bean方法被调用多少次返回的组件都是新创建的,每次都重新创建一例更快)
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
/**
* Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
* @return
*/
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean("tom")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
底层注解-import导入组件
@Import({User.class, DBHelper.class})给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
底层注解-@Conditional条件装配
条件装配:满足Conditional指定的条件,则进行组件注入
@ConditionalOnMissingBean(name = “tom”)//没有tom名字的Bean时,MyConfig类的Bean才能生效
底层注解-@ImportResource导入Spring配置文件
公司使用bean.xml文件生成配置bean,然而你为了省事,想继续复用bean.xml,
@ImportResource(“classpath:beans.xml”)
底层注解-@ConfigurationProperties配置绑定
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用
Spring Boot一种配置配置绑定:
@ConfigurationProperties + @Component
假设有配置文件application.properties
mycar.brand=BYD
mycar.price=100000
@Component
@ConfigurationProperties(prefix = “mycar”)
public class Car {
…
}
只有在容器中的组件,才会拥有SpringBoot提供的强大功能
Spring Boot另一种配置配置绑定:
@EnableConfigurationProperties + @ConfigurationProperties
开启Car配置绑定功能,把这个Car这个组件自动注册到容器中
@EnableConfigurationProperties(Car.class)
public class MyConfig {
…
}
@ConfigurationProperties(prefix = “mycar”)
public class Car {
…
}
自动配置
响应启动类@SpringBootApplication
打开后包含
@SpringBootConfiguration-》打开后@Configuration为一个配置类
@EnableAutoConfiguration-》@AutoConfigurationPackage
- 利用Registrar给容器中导入一系列组件
- 将指定的一个包下的所有组件导入进MainApplication所在包下
@ComponentScan-》为扫描包结构的注解
自动配置-初始加载
@Import(AutoConfigurationImportSelector.class)
这个过程加载了所有的自动配置类,但是!不是所有的自动配置类都存在的,会根据(@Conditional
)按需配置。
利用getAutoConfigurationEntry(annotationMetadata);
给容器中批量导入一些组件。调用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)
获取到所有需要导入到容器中的配置。利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);
得到所有的组件。从META-INF/spring.factories位置来加载一个文件。默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
自动配置-流程
以DispatcherServletAutoConfiguration
的内部类DispatcherServletConfiguration
为例子:
@Bean//导入组件
@ConditionalOnBean(MultipartResolver.class) //容器中有这个类型组件
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中没有这个名字 multipartResolver 的组件
public MultipartResolver multipartResolver(MultipartResolver resolver) {
//给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范,这里会把不规范的类型赋值返回
// Detect if the user has created a MultipartResolver but named it incorrectly
return resolver;//给容器中加入了文件上传解析器;
}
- SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration
每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值。(xxxxProperties里面读取,xxxProperties和配置文件进行了绑定) - 生效的配置类就会给容器中装配很多组件,只要容器中有这些组件,相当于这些功能就有了
- 定制化配置
- 用户直接自己@Bean替换底层的组件
- 用户去看这个组件是获取的配置文件什么值就去修改。
xxxxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ----> application.properties所以直接在自己的application.properties中修改配置就好
一些小插件的方便
lombok
Lombok用标签方式代替构造器、getter/setter、toString()等鸡肋代码。
引入依赖-》寻找依赖过程-》pom.xml点击在parent中点击spring-boot-starter-parent找到spring-boot-dependencies再找版本号搜搜就有了
@Data
@ToString
@EqualsAndHashCode
@Slf4j//日志开发
配置文件
yaml语法
- key: value;kv之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,单引号’’、双引号""表示字符串内容会被 转义、不转义
#对象:键值对的集合。map、hash、set、object
#行内写法:
k: {k1:v1,k2:v2,k3:v3}
k:
k1: v1
k2: v2
k3: v3
#数组:一组按次序排列的值。array、list、queue
#行内写法:
k: [v1,v2,v3]
k:
- v1
- v2
- v3
#实例
@Data
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
@Data
public class Pet {
private String name;
private Double weight;
}
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18
pet:
name: tomcat
weight: 23.4
interests: [篮球,游泳]
animal:
- jerry
- mario
score:
english:
first: 30
second: 40
third: 50
math: [131,140,148]
chinese: {first: 128,second: 136}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 47}
health: [{name: mario,weight: 47}]
web开发
web的静态资源规则制定
-
只要静态资源放在类路径下: called
/static
(or/public
or/resources
or/META-INF/resources
-
访问 : 当前项目根路径/ + 静态资源名
原理: 静态映射/**。
-
请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面。
-
改变默认路径
#改变访问前缀
spring:
mvc:
static-path-pattern: /res/**
#改变默认的放置文件
resources:
static-locations:[classpath:/haha/]
welcome欢迎页和favicon小图标
welcom和favicon都是存储在静态页中
请求处理-rest源码
- @xxxMapping;
- @GetMapping
@PostMapping
@PutMapping
@DeleteMapping - Rest风格支持(使用HTTP请求方式动词来表示对资源的操作)
- @GetMapping
- 以前:/getUser 获取用户/deleteUser 删除用户/editUser 修改用户/saveUser保存用户
现在: /user GET-获取用户DELETE-删除用户PUT-修改用户POST-保存用户 - 核心Filter;HiddenHttpMethodFilter
- 用法:开启页面表单的Rest功能
页面 form的属性method=post,隐藏域 _method=put、delete等(如果直接get或post,无需隐藏域)编写请求映射 - Rest原理(表单提交要使用REST的时候)
表单提交会带上_method=PUT
请求过来被HiddenHttpMethodFilter拦截
请求是否正常,并且是POST
获取到_method的值。
兼容以下请求;PUT.DELETE.PATCH
原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。