博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 知识点整理
阅读量:2157 次
发布时间:2019-05-01

本文共 7200 字,大约阅读时间需要 24 分钟。

1、下面是根据条件初始化bean 

2、读取配置信息操作

加载配置可以用@PropertySource("classpath:com/ecej/test2/test.properties") 记得要用 private Environment environment; 读取配置

package com.ecej.test2; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.Environment; import org.springframework.core.io.Resource; @Configuration @ComponentScan("com.ecej.test2") @PropertySource("classpath:com/ecej/test2/test.properties") public class ELConfig {
@Value("I LOVE YOU") private String normal; @Value("#{systemProperties['os.name']}") private String osName; @Value("#{T(java.lang.Math).random() * 100.0}") private double randomNumber; @Value("#{demoService.another}") private String fromAnother; @Value("#classpath:com/ecej/test2/test.properties") private Resource testFile; @Value("http://www.baidu.com") private Resource testUrl; @Value("${book.name}") private String bookName; @Autowired private Environment environment; @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer(); } public void outputResource() {
try {
System.out.println(normal); System.out.println(osName); System.out.println(randomNumber); System.out.println(fromAnother); System.out.println(IOUtils.toString(testUrl.getInputStream())); System.out.println(bookName); System.out.println(environment.getProperty("book.author")); } catch (Exception e) {
// TODO: handle exception } } }

3、spring event

事件

1 、首先我们要实现ApplicationListener 实现我们自己的监听

2、 定义我们自己的事件 通过集成ApplicationEvent实现

3、 定义config启动

通过applicationContext 发布事件

@Component public class DemoPublisher {
@Autowired private ApplicationContext applicationContext; public void publish(String msg) {
applicationContext.publishEvent(new DemoEvent(this, msg)); } }

4、spring Aware

讲解:bean 和spring是无耦合的,但是如果想用到spring容器的功能资源,就要你的bean知道spring的存在,这就是spring aware

5、多线程

spring通过 TaskExecutor来实现多线程并发编程。使用ThreadPoolExecutor可实现基于线程池的TaskExecutor,使用@EnableAsync开启对异步任务的支持,并通过在实际执行bean方法中使用@Async注解来声明一个异步任务

6、计划任务

通过配置注解@EnableScheduline来开启对计划任务的支持,然后再要执行的任务上加注解@Scheduled

spring通过@Scheduled支持多种类型计划任务,包含cron,fixDelay、fixRate(固定时间执行)

7、条件注解@Conditional

8、组合注解与源注解

源注解:就是可以注解到别的注解上的注解,被注解的注解称之为组合注解。组合注解有源注解的功能。

@Configuration  @ComponentScan 组合出 Wiselyconfiguration

9、@Enable*注解工作原理

10、Spring MVC

下面所讲解的都是配置在MyMVCConfig

通过实现WebApplicationInitializer 等同于web.xml配置

基本配置

spring MVC的定制配置需要我们配置集成一个WebMvcConfigurerAdapter,并在此使用@EnableWebMvc注解,来开启对spring MVC配置支持

静态资源

重写addResourceHandlers方法实现

拦截器配置

类似servlet的Filter

可以让普通bean 实现HanlderIntercepetor接口或者继承HandlerInterceptorAdapter类来实现自定义拦截器

在boot中通过重写WebMvcConfigurerAdapter 的 addInterceptors方法来注册自定义拦截器

@ControllerAdvice


@ExceptionHandler定义全局处理 ,通过value属性可以设置拦截过滤条件

在开发中经常会遇到跳转页面的事情,我们还要单独写一个方法很麻烦,现在可以这样

路径参数配置

在spring mvc中路径参数如果带点“.” ,那点后面的值将被忽略。通过重写configurePathMatch(PathMatchConfigurer) 可不忽略 点后参数

文件上传

demo集合

正式开始spring boot

CLI  命令行控制工具

1、@SpringBootApplication 和入口

这个标签是个组合注解,包含了@Configuration @EnableAutoConfiguration @ComponentScan三个标签

@EnableAutoConfiguration 让spring boot根据类路径中的jar包依赖为当前项目进行自动配置

在spring boot中我们可以使用

@Value("${book.author}")直接注入属性,但是还是感觉一个个注入麻烦啊,SO,我们可以直接映射一个类,用@ConfigurationProperties(prefix="author",locations={"classpath:author.properties"})通过prefix指定前缀,通过locations指定位置

2、spring boot 的web开发

需要定义模版信息的话,使用ViewResolver  ,别忘了在config上加注解@EnableWebMvc

@Bean public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("WEB-INF/classes/views/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); return resolver; }

静态资源默认放在src/main.resources/static 下面

3、静态首页的支持


4、接管spring boot 的web配置

如果boot 提供的配置不是我们需要的,可以通过配置类修改,

注解来实现自己完全控制

@Configuration

@ComponentScan("com.ecej.test.mvc")

@EnableWebMvc

如果我们想即用默认配置,又增加自定义配置,可以集成WebMvcConfigurerAdapter,无需使用@EnableWebMvc注解

5、注册servlet Filter Listener(重点来了)

可以注册ServletRegistrationBean  FilterRegistrationBean  ServletListenerRegistrationBean 的bean来实现

6、Tomcat配置

其实就是servlet容器配置,因为BOOT内置的是tomcat,所以也就叫tomcat配置了

配置都在org.springframework.boot.autoconfigure.web.ServerProperties 中(其实大部分都有这么个配置)

SO,我们只需要在application.properties中配置就好了(如果你想拥别的名字,只需要配置下就行咯,在上边有提到过)。通用的配置都以server作为前缀

例子:

配置容器

server.port=8080

server.session-timeout=2

配置tomcat

server.tomcat.uri-encoding=UTF-8

上边都是配置文件配置,如果想玩玩代码也是可以的,下面介绍代码配置

想配置servlet容器可以实现一个EmbeddedServletContainerCustomizer的接口(注意声明的类要为static)

想直接配置tomcat等则可以直接定义TomcatEmbeddedServletContainerFactory等

7、SSL配置(安全套接层)

生成证书:

JDK下有工具keytool ,他是一个证书管理工具,可以用来生成证书

在你命令执行的当前目录下生成了一个.keystore的证书

配置我们的配置文件

server.port=8000

server.ssl.key-store=.keystore

server.ssl.key-store-password=123456

server.ssl.keyStoreType=JKS

server.ssl.keyAlias:tomcat

此时在启动项目就变成了https的

8、设置自己的Favicon

这个就非常简单了,只需要将自己的favicon.ico文件放在META-INF/resources/   resources/  static/  public/下面任意一个目录下就行了。

9、WebSocket

这到底是个什么鬼呢?

官方说法,就是为浏览器和服务端提供双工异步通信的功能。直接使用WebSocket会使开发非常繁琐的,所以我们使用它的子协议STOMP,它是一个更高级的协议,STOMP协议使用一个基于帧的格式来定义消息,与HTTP的request response类似。

spring boot内置了这玩意,可以看websocket包下的类

需要加入

spring-boot-starter-websocket 包

@EnableWebSocketMessageBroker注解并继承AbstractWebSocketMessageBrokerConfigurer

模式:

广播式  会将消息发送给所有连接了当前的浏览器

代码片段

package com.ecej.demo2.websocket; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; @Configuration @ComponentScan(value = "com.ecej.demo2.websocket") @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override public void registerStompEndpoints(StompEndpointRegistry register) {
register.addEndpoint("/endpointWisely").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry register) {
register.enableSimpleBroker("/topic"); } }

解析:

1、通过@EnableWebSocketMessageBroker 注解开启使用STOMP协议传输基于代理(message broker)的消息,

这时控制器支持使用@MessageMapping,就像使用@RequestMapping一样

2、注册STOMP协议的节点,并映射的指定的URL

3、注册一个STOMP的endpoint,并指定使用SocketJS协议

4、配置消息代理(message broker)

5、广播式应配置一个/topic消息代理

10、spring 的事物机制

spring事物机制提供了一个PlatformTransactionManager接口,不同的数据访问技术的事物使用不同的接口实现


声明式事物

使用@Transactional注解在方法上表明该方法需要事物支持,基于AOP实现操作。

使用@EnableTransactionManagement来开启上边的注解

类级别的注解@Transactional会重载方法级别的

11、缓存支持

不同的缓存技术,需要不同的cacheManager

12、异步消息

spring 对JMS和AMQP的支持分别来自于spring-jms 和spring-rabbit


他们分布需要ConnectionFactory来实现连接消息代理,并分别提供了JmsTemplate、RabbitTemplate

spring为JMS 、AMQP提供了@JmsListener @RabbitListener 注解在方法上监听消息代理发布的消息。我们只需要分别通过@EnableJms @EnableRabbit开启支持

转载地址:http://vfmwb.baihongyu.com/

你可能感兴趣的文章
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST09~10-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST07~08-----P5~6
查看>>
solver及其配置
查看>>
JAVA多线程之volatile 与 synchronized 的比较
查看>>
Java集合框架知识梳理
查看>>
笔试题(一)—— java基础
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>