提高效率的 @EnableJdbcAuditing

@EnableJdbcAuditing 是 Spring Data JDBC 提供的一个注解,用于启用审计功能(Auditing)。它主要负责自动填充实体类中的审计字段,比如 createdBy、createdDate、lastModifiedBy 和 lastModifiedDate。 以下从功能、实现逻辑和源码解析三个方面详细说明。 1. 功能 @EnableJdbcAuditing 提供以下功能: 自动填充创建和修改时间:在插入或更新记录时,自动填充实体类中的时间字段(如 createdDate 和 lastModifiedDate)。 支持用户信息:可以自动填充创建者和修改者信息(如 createdBy 和 lastModifiedBy),需要配合自定义 AuditorAware 实现。 2. 使用示例 数据库表 假设有一张用户表 user: CREATE TABLE user ( id BIGINT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), created_by VARCHAR(50), created_date TIMESTAMP, last_modified_by VARCHAR(50), last_modified_date TIMESTAMP ); 实体类 定义对应的实体类,标注审计字段: import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; […] →Read more

Agile Testing Quadrants

四个维度 面向业务 面向技术 指导开发 指导产品 理想情况下, 我们希望以下 3 个象限是自动化的 Q1. 功能性验收测试 Q3. 单元/集成/系统测试 Q4. 非功能性验收测试(性能,压力,安全测试) →Read more

DNS-F

DNS-F (DNS Failover) is a DNS-based mechanism supported by Nacos, a dynamic service discovery and configuration management platform, to provide failover support for service discovery. It works by allowing services to use DNS queries to retrieve IP addresses of available service instances, ensuring resilience and reliability when instances become unavailable. Here’s a breakdown of what […] →Read more

The new and revised 15 factors

12 factor In the modern era, software is commonly delivered as a service: called web apps, or software-as-a-service. The twelve-factor app is a methodology for building software-as-a-service apps that: Use declarative formats for setup automation, to minimize time and cost for new developers joining the project; Have a clean contract with the underlying operating system, […] →Read more

RAG Architecture

A typical RAG application has two main components: Indexing: a pipeline for ingesting data from a source and indexing it. This usually happens offline. Retrieval and generation: the actual RAG chain, which takes the user query at run time and retrieves the relevant data from the index, then passes that to the model. The most […] →Read more

EnableAutoConfiguration 注解的原理

在 Spring Boot 中,@EnableAutoConfiguration 是一个核心注解,它实现了自动配置的功能。它的作用是根据项目中依赖的库以及应用上下文中的配置,自动配置 Spring 应用程序的上下文。以下是它的原理和运行机制: 1. @EnableAutoConfiguration 的定义 @EnableAutoConfiguration 注解本质上是一个复合注解,其源码如下: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { Class<?>[] exclude() default {}; String[] excludeName() default {}; } 主要包括以下两部分: • @AutoConfigurationPackage: 自动将当前类所在包及其子包中的所有组件注册到 Spring 容器。 • @Import(AutoConfigurationImportSelector.class): 导入了 AutoConfigurationImportSelector,用来加载所有可用的自动配置类。 2. 自动配置的核心机制 2.1 自动配置类的定义 自动配置类通过 spring.factories 文件定义,这个文件通常位于 META-INF 目录下。例如: org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration • 每个类都是一个自动配置类,Spring […] →Read more

Cloud Native

Cloud native practices empower organizations to develop, build, and deploy workloads in computing environments (public, private, hybrid cloud) to meet their organizational needs at scale in a programmatic and repeatable manner. It is characterized by loosely coupled systems that interoperate in a manner that is secure, resilient, manageable, sustainable, and observable. Cloud native technologies and […] →Read more

we live by an invisible sun within us

Life is a pure flame, and we live by an invisible sun within us 生命是一团纯净的火焰, 我们依靠自己内心看不见的太阳而生存 The vast, th unbounded prospect lies before us. →Read more

mybatis spring boot starter 是怎么做的

Overview 要与 Spring 一起使用 MyBatis,你至少需要一个 SqlSessionFactory 和一个 mapper 接口。 MyBatis-Spring-Boot-Starter 将会: 自动探测存在的 DataSource 将使用 SqlSessionFactoryBean 创建并注册一个 SqlSessionFactory 的实例,并将探测到的 DataSource 作为数据源 将创建并注册一个从 SqlSessionFactory 中得到的 SqlSessionTemplate 的实例 自动扫描你的 mapper,将它们与 SqlSessionTemplate 相关联,并将它们注册到Spring 的环境(context)中去,这样它们就可以被注入到你的 bean 中 MyBatis-Spring-Boot-Starter 是一个开箱即用的解决方案,用于将 MyBatis 与 Spring Boot 集成。它简化了 MyBatis 的配置和使用,同时充分利用了 Spring 的依赖注入和生命周期管理功能。以下是它的实现原理的详细解析: 自动配置的实现 MyBatis-Spring-Boot-Starter 利用了 Spring Boot 的自动配置机制,通过 @EnableAutoConfiguration 和 spring.factories 文件实现自动化配置。 spring.factories MyBatis-Spring-Boot-Starter […] →Read more

道与术:程序员的内功心法

在软件开发的江湖中,C/C++ 和 Java 就像少林与武当,一个偏向“硬功夫”,刀枪棍棒样样能打;另一个更讲究“以柔克刚”,推崇灵活多变的内功。而 Python、Go 和 Rust 则是近年崛起的少年高手,一个擅长速成、一个拥抱云原生,还有一个以内存安全闻名,都在争夺编程江湖的“武林盟主”之位。 尤其是在企业开发的武林中,Java 堪称“中原武林盟主”,它的招式(术)浩如烟海,从简单的“打狗棒法”(工具类库)到深奥的“九阴真经”(分布式架构)。门下弟子修炼轮子(第三方库)无数。然而,纵然 Java 的术法浩如烟海,如果只沉迷于术,终究如蜻蜓点水,难入武道真谛。只练招式不修内功,就像拿着绝世神兵却没有力气挥动,最终难逃江湖纷争的淘汰法则。 正如古人所言:“术乃器,道为心。器可换,道难变。” 术是什么?术是刀枪剑戟的武器。 术是工具、框架、库、配置文件。你可以把术看成是你手中的菜刀,某个问题来了,直接拿出来用,快速解决,效率拉满。 比如: • Spring:一个开箱即用的武林秘籍,简直是厨师的多功能锅,煎炒烹炸样样行。 • MyBatis:数据库操作的助力,能让你用 SQL 潇洒如风,甚至可以用 XML 来写代码。 • Spring Security:防盗、防抢、防 SQL 注入三件套。 • WebFlux:响应式编程的“闪电五连鞭”,性能杠杠的。 术学起来很爽,用起来很炫,解决问题很快,但如果你只会用术,可能就像一个拿着高级武器但内功薄弱的武者,风吹草动就可能翻车。 道是什么?道是你的内功心法。 道是一种理念、一种本质,超脱于术之上。它是程序员的大局观,是问题背后的普适法则。懂得了道,你就能从根本上理解术为什么存在、怎么优化甚至怎么重写它。 比如: • 依赖注入和控制反转(IoC) 是道,理解了它,你就能明白 Spring 为何如此设计。 • ORM 的本质 是道,懂了它,你就知道 JPA 和 MyBatis 的优劣。 • CAP 理论 是道,掌握了它,分布式系统中的“妥协艺术”尽在掌握。 • 响应式编程的多路复用原理 是道,搞懂了它,WebFlux […] →Read more