当前位置: 首页 > news >正文

读spring官方文档的一些关键知识点介绍

目录

    • bean definition
    • BeanPostProcessor
    • BeanFactoryPostProcessor
    • @Component and Further Stereotype Annotations
    • AOP Concepts

bean definition

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans-child-bean-definitions

A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information, such as the initialization method, a static factory method name, and so on. A child bean definition inherits configuration data from a parent definition. The child definition can override some values or add others as needed. Using parent and child bean definitions can save a lot of typing. Effectively, this is a form of templating.

bean定义包括了,构造参数、属性值、初始化方法等。可以有父子继承

BeanPostProcessor

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans-factory-extension

The BeanPostProcessor interface defines callback methods that you can implement to provide your own (or override the container’s default) instantiation logic, dependency-resolution logic, and so forth. If you want to implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean, you can plug in one or more BeanPostProcessor implementations.

You can configure multiple BeanPostProcessor instances, and you can control the order in which these BeanPostProcessor instances execute by setting the order property. You can set this property only if the BeanPostProcessor implements the Ordered interface. If you write your own BeanPostProcessor, you should consider implementing the Ordered interface, too. For further details, see the javadoc of the BeanPostProcessor and Ordered interfaces. See also the note on programmatic registration of BeanPostProcessor instances.

定制化bean, 可以自己去实现bean的实例化、依赖解决。bean的实例化,属性配置,初始化方法都可以进行定制。

自定义BeanPostProcessor需要实现BeanPostProcessor相关接口,并有Order相关接口,可实现按顺序访问。

example:

import org.springframework.beans.factory.config.BeanPostProcessor;public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {// simply return the instantiated bean as-ispublic Object postProcessBeforeInitialization(Object bean, String beanName) {return bean; // we could potentially return any object reference here...}public Object postProcessAfterInitialization(Object bean, String beanName) {System.out.println("Bean '" + beanName + "' created : " + bean.toString());return bean;}
}

BeanFactoryPostProcessor

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans-factory-extension-factory-postprocessors

BeanFactoryPostProcessor operates on the bean configuration metadata. That is, the Spring IoC container lets a BeanFactoryPostProcessor read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessor instances.

You can configure multiple BeanFactoryPostProcessor instances, and you can control the order in which these BeanFactoryPostProcessor instances run by setting the order property. However, you can only set this property if the BeanFactoryPostProcessor implements the Ordered interface. If you write your own BeanFactoryPostProcessor, you should consider implementing the Ordered interface, too. See the javadoc of the BeanFactoryPostProcessor and Ordered interfaces for more details.

BeanFactoryPostProcessor操作bean配置元数据,可以读取bean配置,可以bean实例化之前改变元数据,即对BeanDefinition进行干预。

自定义BeanFactoryPostProcessor需要实现BeanFactoryPostProcessor相关接口,并有Order相关接口,可实现按顺序访问

@Component and Further Stereotype Annotations

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans-stereotype-annotations

The @Repository annotation is a marker for any class that fulfills the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception Translation.

Spring provides further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases (in the persistence, service, and presentation layers, respectively). Therefore, you can annotate your component classes with @Component, but, by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts. @Repository, @Service, and @Controller can also carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated earlier, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

  • @Service
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component 
public @interface Service {// ....
}

The Component causes @Service to be treated in the same way as @Component.

AOP Concepts

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#aop-introduction-defn

These terms are not Spring-specific. Unfortunately, AOP terminology is not particularly intuitive. However, it would be even more confusing if Spring used its own terminology.

  • Aspect: A modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented by using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

  • Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

  • Advice: Action taken by an aspect at a particular join point. Different types of advice include “around”, “before” and “after” advice. (Advice types are discussed later.) Many AOP frameworks, including Spring, model an advice as an interceptor and maintain a chain of interceptors around the join point.

  • Pointcut: A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

  • Introduction: Declaring additional methods or fields on behalf of a type. Spring AOP lets you introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

  • Target object: An object being advised by one or more aspects. Also referred to as the “advised object”. Since Spring AOP is implemented by using runtime proxies, this object is always a proxied object.

  • AOP proxy: An object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy is a JDK dynamic proxy or a CGLIB proxy.

  • Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

Spring AOP includes the following types of advice:

  • Before advice: Advice that runs before a join point but that does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

  • After returning advice: Advice to be run after a join point completes normally (for example, if a method returns without throwing an exception).

  • After throwing advice: Advice to be executed if a method exits by throwing an exception.

  • After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

  • Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

http://www.lryc.cn/news/523678.html

相关文章:

  • 2024年AI与大数据技术趋势洞察:跨领域创新与社会变革
  • ThinkPhp项目解决静态资源请求的跨域问题的解决思路
  • mybatis的多对一、一对多的用法
  • 消息队列实战指南:三大MQ 与 Kafka 适用场景全解析
  • 前端发送Ajax请求的技术Axios
  • 第17章:Python TDD回顾与总结货币类开发
  • opencv_KDTree_搜索介绍及示例
  • Windows 上安装 MongoDB 的 zip 包
  • 先进制造aps专题二十七 西门子opcenter aps架构分析
  • 【数据分享】1929-2024年全球站点的逐年平均气温数据(Shp\Excel\无需转发)
  • 机器学习——什么是代价函数?
  • docker 部署 MantisBT
  • 02内存结构篇(D1_自动内存管理)
  • Centos 8 交换空间管理
  • “深入浅出”系列之数通篇:(5)TCP的三次握手和四次挥手
  • 接口测试及接口测试常用的工具
  • 使用rpc绕过咸鱼sign校验
  • NPC与AI深度融合结合雷鸟X3Pro AR智能眼镜:引领游戏行业沉浸式与增强现实新纪元的畅想
  • 【物联网】ARM核介绍
  • Android系统定制APP开发_如何对应用进行系统签名
  • Tesla Free-Fall Attack:特斯拉汽车网络安全事件纪要
  • 网络安全工程师学习路线
  • 分区和分表有什么区别?
  • C#,入门教程(01)—— Visual Studio 2022 免费安装的详细图文与动画教程
  • 简述mysql 主从复制原理及其工作过程,配置一主两从并验证。
  • Android 右键后无Java class创建
  • 【从零开始入门unity游戏开发之——C#篇46】C#补充知识点——命名参数和可选参数
  • 哈尔滨有双线服务器租用吗?
  • JVM 面试八股文
  • STM32 FreeRTOS内存管理简介