抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Aware

BeanNameAware

beanNameAware可以获得容器中Bean的名称,作用于每一个Bean。当bean被创建的时候设置他的名字,在基本properties填充完成以后,init调用前执行

摘自: spring-beans:5.3.4 org.springframework.beans.factory.BeanNameAware

Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback such as {@link InitializingBean#afterPropertiesSet()} or a custom init-method.

1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;

@Component
public class BeanNameAwareDemo implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println(name);
}
}

Spring Boot Starter Webflux

如何编写

理清依赖

自动配置

@Configuration 指定这个是配置类
@ConditionalOnxxx 在某些条件下才生效
@AutoConfigureAfter 指定自动配置类的顺序
@Bean 给IOC加组件
@ConfiguretionProperties 结合相关的xxxProperties配置类来绑定配置
@EnableConfigurationProperties 让xxxProperties生效加入到容器中
讲自动配置类配置在META-INF/spring.factories中

启动配置原理

几个重要的事件回调机制

  • ApplicationContextInitializer
  • SpringApplicationRunListener
  • ApplicationRunner
  • CommandLineRunner

启动流程

1
return new SpringApplication(primarySources).run(args);
  • 创建SpringApplication对象
  • 运行run方法

创建对象

现在左边的参数是null

1
2
3
4
5
6
7
8
9
10
11
12
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}

创建项目

选择MySQL+JDBC+Web

链接数据库

1
2
3
4
5
6
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver

需求

员工列表

|普通CRUD|restfulCRUD
-|-|-
查询|getEmp|emp…GET
添加|addEmp?|emp…POST
修改|updateEmp?|emp/{id}…PUT
删除|deleteEmp?|emp/{id}…DELETE

国际化

  • 编辑国际化配置文件
  • 使用ResourceBundleMessageSource管理国际化资源文件
  • 在页面使用fmt:message取出国际化内容

扩展SpringMVC

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:view-controller path="/hello" view-name="succcess"></mvc:view-controller>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>

模版引擎

常见的模版引擎有JSP,Velocity,Freemarker,Thymeleaf

SpringBoot推荐的Thymeleaf

1
2
3
4
5
<!--        模版引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

视频中说这个版本有点低,是2.16的
然鹅我用的SpringBoot2,已经是3.x了
修改版本号,这招估计学了有用,这个能覆盖版本

SpringBoot与Web

先在idea中选择场景
SpringBoot已经默认将这些常见配置好了,我们只需要在配置文件中指定少量配置就可以运行起来
然后我们可以开始编写业务代码了

SpringBoot与静态资源

WebMvcAutoConfiguration

打开WebMvcAutoConfiguration.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}