美文网首页Java技术升华我爱编程Spring
【原】SpringBoot知识点——Spring基础篇

【原】SpringBoot知识点——Spring基础篇

作者: 曹赫洋 | 来源:发表于2018-04-17 11:29 被阅读230次

Spring基础

1.1 配置元数据

xml配置,注解配置,java配置都被成为配置元数据。

元数据即描述数据的数据,元数据本身不具备任何可执行的能力,只能通过外界代码来对这些元数据进行解析之后才有意义。

1.2 Bean的Scope

  • singleton: 在Spring的IoC容器中只存在一个对象实例,所有该对象的引用都共享这个实例。Spring 容器只会创建该bean定义的唯一实例,这个实例会被保存到缓存中,并且对该bean的所有后续请求和引用都将返回该缓存中的对象实例,一般情况下,无状态的bean使用该scope。

  • prototype:每次对该bean的请求都会创建一个新的实例,一般情况下,有状态的bean使用该scope。

  • request:每次http请求将会有各自的bean实例,类似于prototype。

  • session:在一个http session中,一个bean定义对应一个bean实例。

  • global session:在一个全局的http session中,一个bean定义对应一个bean实例。典型情况下,仅在使用portlet context的时候有效。

说明:spring的默认scope(bean作用域)是singleton。

1.3 Spring EL和资源调用,配置文件注入

注入配置文件需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的bean。

1.4 Spring Bean的初始化和销毁

  • 使用@PostConstruct注解初始化,使用@PreDestroy注解销毁Bean;

  • 实现InitializingBean, DisposableBean这两个接口,并复写afterPropertiesSet()和destroy()方法;

  • 使用init-method和destroy-method配置方法,xml和@Bean的方式都可以用;

1.5 Profile

几种配置方式:

  • application.properties中设置,eg: spring.profiles.active=dev;

  • 命令行激活,eg:java -jar xxx.jar –spring.profiles.active=test;

  • 基于java的@Profile注解,任何@Component或@Configuration都能被@Profile标记;

  • 编程的方式激活,通过设定Environment的ActiveProfiles;

例如:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        applicationContext.getEnvironment().setActiveProfiles("dev");
    }
}

1.6 Spring事件(Application Event)

spring事件(application event)为Bean与Bean之间的消息通信添加了支持,当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要另外一个Bean监听当前Bean所发送的事件。

spring的事件需要遵循以下流程:

(1)自定义事件:继承ApplicationEvent

(2)定义事件监听器:实现ApplicationListener

(3)使用容器发布事件

1.7 Spring Aware

Spring Aware的目的是为了让Bean获得Spring容器的服务

Aware Class 说明
BeanNameAware 获得到容器中Bean的名称
BeanFactoryAware 获得当前bean Factory,从而调用容器的服务
MessageSourceAware 得到message source从而得到文本信息
ApplicationEventPublisherAware 应用时间发布器,用于发布事件
ResourceLoaderAware 获取资源加载器,可以获得外部资源文件

1.8 多线程

  • 关键注解 @EnableAsync:Configuration中开启多线程
  • @Async:定义方法为异步方法;
  • 关键接口AsyncConfigurer;
  • 关键类TaskExecutor,ThreadPoolTaskExecutor。

1.9 定时任务

  • @EnableScheduling:Configuration中开启定时任务
  • @Scheduled:配置定时任务

1.10 条件注解@Conditional

1.11 组合注解与原注解

1.12 Enable注解的工作原理*

Spring MVC基础

2.1 DispatcherServlet的理释

2.2 Spring Mvc配置

  • @EnableWebMvc 开启一些默认配置,如ViewResolver或者MessageConverter等。
  • WebApplicationInitializer,SpringServletContainerInitializer的理解
  • 定制配置:配置类必须继承WebMvcConfigurerAdapter类,并且使用@EnableWebMvc注解标注,重写WebMvcConfigurerAdapter的方法可以达到定制化配置的效果;
  • Interceptor拦截器
  • ControllerAdvice:@ControllerAdvice是一个@Component,用于定义@ExceptionHandler,@InitBinder(具体方法执行前始化数据绑定器)和@ModelAttribute方法,适用于所有使用@RequestMapping方法;
  • 配置类中重写addViewControllers简化页面跳转的配置;
  • 配置类中重写configurePathMatch配置处理spring mvc对url不能有.的限制;
  • 文件上传配置:MultipartResolver
  • HttpMessageConverter:用来处理request和response里的数据,可以用来搞json数据的加解密,*WebMvcConfigurerAdapter里有两个方法可以重写用来注册自定义的HttpMessageConverter,分别是 extendMessageConverters(重载会覆盖Spring MVC默认注册的多个HttpMessageConverter)和 configureMessageConverters(仅添加一个自定义的HttpMessageConverter,不会覆盖默认的HttpMessageConverter);

2.3 服务器端推送技术

1.基于SSE(Server Send Event服务端发送事件)的服务器推送 只有chrome firefox有效,controller方法的produces = "text/event-stream;charset=UTF-8"

2.基于Servlet3.0+的异步方法特性 跨浏览器

2.4 测试 TTD

相关文章

网友评论

    本文标题:【原】SpringBoot知识点——Spring基础篇

    本文链接:https://www.haomeiwen.com/subject/xwyukftx.html