• spring framework notes


    Overview

    The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.

    A key element of Spring is infrastructural support at the application level: Spring focuses on the “plumbing” of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

    programming and configuration model: @Compoent, @Configuration 这两个是spring的核心。spring负责component之间的依赖关系以及注入,以及配置的产生和注入。
    A key element of Spring is infrastructural support at the application level:让component只用关心自己的业务逻辑,而不用关心装配过程。
    spring 将配置,依赖和业务逻辑分离,很好的支持了component组件的灵活插拔。比如test和product可以enable不同的component和configuration,以实现不同的功能。不过依赖是没有办法分开的,毕竟都在一个jar里面,所以只能手动在配置文件里面disable。

    The Spring Framework is divided into modules. Applications can choose which modules they need. At the heart are the modules of the core container, including a configuration model and a dependency injection mechanism: 如上,spring提供的主要功能就是组件的管理,也就是container。组件有包括业务组件@Component以及配置组件@Configuration。

    Design Philosophy

    When you learn about a framework, it’s important to know not only what it does but what principles it follows. Here are the guiding principles of the Spring Framework:

    1. Provide choice at every level. Spring lets you defer design decisions as late as possible. For example, you can switch persistence providers through configuration without changing your code. The same is true for many other infrastructure concerns and integration with third-party APIs.
      这既是一个优点也是一个缺点吧。因为在不同的deployment都是一样的jar,要实现不同的功能只能通过配置文件了。

    2. Accommodate diverse perspectives. Spring embraces flexibility and is not opinionated about how things should be done. It supports a wide range of application needs with different perspectives.
      这个有点useless的味道,并且不一定是个好东西,选择太多也会带来混乱。 Spring Boot provides a quick (and opinionated) way to create a production-ready Spring-based application. It is based on the Spring Framework, favors convention over configuration, and is designed to get you up and running as quickly as possible. 大家都用spring-boot吧。
      convention over configuration 是个好定西,大家都遵守一样的convention就能更简洁,但是有的代码写的不好,不遵守convention就会更难debug。

    3. Maintain strong backward compatibility. Spring’s evolution has been carefully managed to force few breaking changes between versions. Spring supports a carefully chosen range of JDK versions and third-party libraries to facilitate maintenance of applications and libraries that depend on Spring.
      兼容性对生产服务来说很重要,不然没人敢升级了

    4. Care about API design. The Spring team puts a lot of thought and time into making APIs that are intuitive and that hold up across many versions and many years.
      api设计优秀?

    5. Set high standards for code quality. The Spring Framework puts a strong emphasis on meaningful, current, and accurate javadoc. It is one of very few projects that can claim clean code structure with no circular dependencies between packages.
      代码质量好。循环依赖在java很常见,其实java应该在语言层面解决这个问题,但是java又支持动态生成和允信class文件,所以编译器也不能完全规避这种情况。

    3,4,5算是开源软件的common sense吧。但是spring作为java backend的事实标准,确实有吹牛逼的资本。

    core

    1. The IoC Container

    Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container. A thorough treatment of the Spring Framework’s IoC container is closely followed by comprehensive coverage of Spring’s Aspect-Oriented Programming (AOP) technologies.
    spring的核心是IOC容器, spring对容器的加强主要是spring自有的一套AOP proxy, AOP是spring实现声明式编程的主要手段。

    Spring也可以和AspectJ组合使用。

    1.2. Container Overview

    It is a process whereby objects define their dependencies。The container then injects those dependencies when it creates the bean。
    应用程序声明自己的依赖(@Autowire),spring负责初始化以及注入依赖。基本上就是声明式编程的那一套。
    ApplicationContext is a sub-interface of BeanFactory. It adds:

    Easier integration with Spring’s AOP features

    Message resource handling (for use in internationalization)

    Event publication

    Application-layer specific contexts such as the WebApplicationContext for use in web applications.

    Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

    The configuration metadata is represented in XML, Java annotations, or Java code.
    Spring bean的配置和依赖可以通过xml配置也可以通过annotation或者实现各种XXXConfigurer接口的配置类。

    Indeed, your application code should have no calls to the getBean() method at all and thus have no dependency on Spring APIs at all. 业务代码与框架完全分离,仅仅通过xml配置来组合是最理想的状态,最灵活但是有时候也很麻烦,使用springboot我们依赖了框架提供的annotation(@Component, @Autowire),但是也比手动使用框架的操作bean的api要好。

  • 相关阅读:
    实战:如何优雅的从 Skywalking 切换到 OpenTelemetry
    Tomcat 架构
    支付宝H5支付
    【Django 04】Serialization 序列化的高级使用
    EDA工具开发中的调参方法
    高通Android 12/13 设置和获取ADB状态
    【总】HEC-RAS学习记录
    containerd 镜像构建工具 -- nerdctl 和 buildkit
    envoy开发调试环境搭建
    算法通关村——字符串反转问题解析
  • 原文地址:https://blog.csdn.net/weixin_45594127/article/details/126223725