Java 官方笔记 10 注解

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 注解 注解的作用: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth. Runtime processing — Some annotations are available to be examined at runtime. 注解格式,使用@: @Entity 带 key-value: @Author( name = "Benjamin Franklin", date = "3/27/2003" ) class MyClass { ... } 只有 1 个 key 时可以省略 key: ...

跟着官方文档学 Maven 构建生命周期

在 IntelliJ IDEA 中,显示了 Maven 的 Lifecycle:

序列赋值引发的 Python 列表陷进

序列赋值是 Python 默认操作,如果使用不当,有可能会掉入语法陷阱。

DRF 终极封装 ViewSet 和 Router 附教程 PDF 源码

在 DRF 官方教程的学习过程中,一个很明显的感受是框架在不断地进行封装,我们自己写框架/工具/脚本/平台也可以模仿模仿,先完成底层代码,再做多层封装,让使用者很容易就上手操作。本文是教程的最后一篇,介绍 ViewSets 和 Routers。

Java 官方笔记 9Lambda 表达式

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Lambda Expression 有了 Lambda Expression,就不用再写 anonymous classes。 写 Lambda,首先要找到它的类型。 There is a restriction on the type of a lambda expression: it has to be a functional interface. 函数接口,只有 1 个抽象方法的接口: @FunctionalInterface public interface Runnable { public abstract void run(); } 默认方法 do not count,所以下面这个也是函数接口: @FunctionalInterface public interface Consumer<T> { void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) { // the body of this method has been removed } } 静态方法也 do not count,所以下面这个还是函数接口: ...

对 IoC 容器和 Bean 的学习笔记

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 What We Mean by “Spring” The term “Spring” means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started. Over time, other Spring projects have been built on top of the Spring Framework. Most often, when people say “Spring”, they mean the entire family of projects. This reference documentation focuses on the foundation: the Spring Framework itself. ...

能取值亦能赋值的 Python 切片

切片,就像面包,给几刀,切成一片一片,可以做成吐司,也可以做成三明治,口味更佳:

DRF 使用超链接 API 实现真正 RESTful

很多 API 并不是真正的实现了 RESTful,而应该叫做 RPC (Remote Procedure Call 远程过程调用),Roy Fielding 曾经提到了它们的区别,原文如下:

Java 官方笔记 8 泛型

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 泛型 为什么需要泛型?generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. 说白了就像 Python 动态语言的变量,是动态的,可以指向任意类型。 泛型有个好处是不需要类型转换: List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); List<String> list = new ArrayList<String>(); list.add("hello"); String s = list.get(0); // no cast 这个例子的泛型,是指 List 的实现使用了泛型<T>,从而给使用带来了好处。 定义 class name<T1, T2, ..., Tn> { /* ... */ } 比如定义class List<T>,使用List<String> 将以下代码: public class Box { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; } } 改为泛型实现: ...

JavaWeb 开发进阶 SpringMVC 的 DispatcherServlet

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 DispatcherServlet 简介 DispatcherServlet是 Spring MVC 框架的核心组件,充当前端控制器(Front Controller)的角色。它负责接收所有的 HTTP 请求,并将它们分发给适当的处理器(控制器)。DispatcherServlet是整个 Spring MVC 应用程序的入口点,管理请求的整个生命周期。 通过源码查看 DispatcherServlet 定义: public class DispatcherServlet extends FrameworkServlet public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware DispatcherServlet 继承了 FrameworkServlet,继承了 HttpServletBean,继承了 HttpServlet。HttpServlet 来自jakarta.servlet.http.HttpServlet,也就是新版 Servlet: <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.1.0</version> <scope>provided</scope> </dependency> 果然,Spring MVC 是基于 Servlet 的! DispatcherServlet 注册 DispatcherServlet 是个 Servlet,需要注册后才能使用。有 3 种注册方式:web.xml、Spring MVC、Spring Boot ① web.xml <web-app> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/app-context.xml</param-value> </context-param> <servlet> <servlet-name>app</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>app</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> </web-app> ② Spring MVC ...