0%

JavaWeb过滤器

过滤器 拦截器 区别

过滤器基于JavaWeb

  • filter-serverlet-intercepter-controller

拦截器基于Intercepter

如何注册

web.xml

@WebFilter

FilterRegisterBean+Filter

  • 注册
  • ServletContext Initializer
  • Filter definition
  • addRegister

设计模式

  • 责任链
    • FilterChain
    • FilterConfig

  • BeanFactoryPostProcessor
  • BeanDefinitionRegistryPostProcessor
  • BeanPostProcessor
  • InitializingBean
  • ApplicationContextAware
  • ApplicationListener
阅读全文 »

Springboot自定义注解

BeanPostProcessor的应用案例

  • 使用@interface定义注解
  • 使用BeanPostProcessor处理注解

BeanPostProcessor为bean后置处理器. Spring启动过程中,所有的bean初始化的时候会调用bean的后置处理器.

自定义注解类

1
2
3
4
5
6
7
8
9
10
import org.springframework.stereotype.Component;
import java.lang.annotation.*;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Test {
String value() default "";
}
阅读全文 »

常规方式

  • 通过构造方法实例化

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="test" class="com.learn.Test" />
    <bean id="test1" class="com.learn.Test">
    <constructor-arg name="id" value="1"/>
    </bean>
    </beans>
    1
    2
    3
    4
    5
    6
    7
    8
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class DemoApplication {
    public static void main(String[] args) {
    ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("beans.xml");
    Test testA=(Test)ca.getBean("test");
    }
    }
阅读全文 »

Spring中的BeanDefinition

BeanDefinition 是 Spring Framework 中定义 Bean 的配置元信息接口.BeanDefinition描述一个bean. 包括bean的属性,构造函数参数列表,依赖bean,是否是单例,bean的类名等等

BeanDefinition元信息

属性 (Property) 说明
Class Bean全类名,必须是具体类,不能用抽象类或接口
Name Bean的名称或者ID
scope Bean的作用域 (如: singleton, prototype 等)
Constructor arguments Bean构造器参数(用于依赖注入)
Properties Bean属性设置(用于依赖注入)
Autowiring mode Bean自动绑定模式(如:通过名称byName)
Lazy initialization mode Bean延迟初始化模式(延迟和非延迟)
Initialization method Bean初始化回调方法名称
Destruction method Bean销毁回调方法名称
阅读全文 »

List

LinkedList vs ArrayList

Internal implementation

这两个集合都允许重复的元素并维持元素的插入顺序。

LinkedList使用doubly-linked list LinkedList实现它。 ArrayList使用dynamically resizing array实现它。 这将导致性能上的进一步差异。

Performance

Add operation

如果不需要调整Array的大小,则在ArrayList中添加元素是O(1)操作。 如果调整数组大小,则它变为O(log(n))

在LinkedList中添加元素是O(1)操作,因为它不需要任何导航。

阅读全文 »

什么是微服务架构

微服务架构就是将单体的应用程序分成多个应用程序,这多个应用程序就成为微服务,每个微服务运行在自己的进程中,并使用轻量级的机制通信。这些服务围绕业务能力来划分,并通过自动化部署机制来独立部署。这些服务可以使用不同的编程语言,不同数据库,以保证最低限度的集中式管理。

Spring Cloud 是什么

  • Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、智能路由、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。
  • Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

什么是Spring Cloud Alibaba?

Spring Cloud Alibaba 套件,阿里开源组件、阿里云商业组件整合进 Spring Cloud 体系当中,同时对 Spring Cloud Gateway、OpenFeign、Ribbon 等等进行集成。
主要功能如下:

  • 服务注册与发现:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。
  • 分布式配置管理:支持分布式系统中的外部化配置,配置更改时自动刷新。
  • 服务限流降级:默认支持 WebServlet、WebFlux, OpenFeign、RestTemplate、Spring Cloud Gateway, Zuul, Dubbo 和 RocketMQ 限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级 Metrics 监控。
  • 消息驱动能力:基于 Spring Cloud Stream 为微服务应用构建消息驱动能力。
  • 分布式事务:使用 @GlobalTransactional 注解, 高效并且对业务零侵入地解决分布式事务问题。

Spring Cloud Alibaba和Spring Cloud 的区别和联系

Spring Cloud Alibaba是Spring Cloud的子项目,Spring Cloud Alibaba符合Spring Cloud标准。
随着Spring Cloud Netflix下的相关组件逐步进入维护模式,进入维护模式的意思就是从目前一直到以后一段时间Spring Cloud Netflix提供的服务和功能就这么多了,不再开发新的组件和功能了。同时Spring Cloud官方也在积极孵化其他替代品,以满足Spring Cloud版本迭代的需求。
Spring Cloud Alibaba生态下的各个组件其实在国内很多公司很早之前就有使用,它们在服务治理方面比Spring Cloud Netflix更加强大,而且比较符合国内互联网公司的业务特性,推荐使用。

阅读全文 »

SpringBoot 面试题

Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?

启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含 了以下 3 个注解

@SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。

@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项

@ComponentScan:Spring组件扫描。

SpringBoot 生命周期

SpringBoot应用的生命周期,整体上可以分为SpringApplication初始化阶段、SpringApplication运行阶段、SpringApplication结束阶段、SpringBoot应用退出四个阶段。

阅读全文 »

Spring-面试题

什么是嵌入式服务器?

为什么要使用嵌入式服务器

  • 外部服务器写完代码后需打包额外部署,需要额外学习部署内容
  • Springboot内置Tomcat服务器,使用java -jar方式,将web工程变成基本java工程,简化开发部署方式

使用Spring的优势

  • 通过DI、AOP消除样板式代码简化企业开发

  • 低代码侵入性

  • 基于Spring框架开发的代码,可以独立于各种应用服务器

  • IOC降低业务对象替换复杂性,提高解耦性

  • AOP支持将通用任务集中处理,提高复用能力

  • 生态强大,领域范围大,与第三方框架整合,组件众多,简化开发

  • 组件化功能,可以自定义引入所需组件,降低代码臃肿

阅读全文 »