diff --git a/spring/spring-aop-annotation/README.md b/spring/spring-aop-annotation/README.md index ce8be68..ea42eb6 100644 --- a/spring/spring-aop-annotation/README.md +++ b/spring/spring-aop-annotation/README.md @@ -1,37 +1,35 @@ -# spring AOP(注解方式) +# Spring AOP(注解方式) -## 目录
+ ## 一、说明 ### 1.1 项目结构说明 -1. 切面配置位于 com.heibaiying.config 下 AopConfig.java 文件; -2. 自定义切面位于 advice 下,其中 CustomAdvice 是标准的自定义切面,FirstAdvice 和 SecondAdvice 用于测试多切面共同作用于同一个被切入点时的执行顺序; -3. OrderService 是待切入方法。 +1. 切面配置位于 com.heibaiying.config 下 `AopConfig` ; +2. 自定义切面位于 advice 包下,其中 `CustomAdvice` 是标准的自定义切面,`FirstAdvice` 和 `SecondAdvice` 用于测试多切面共同作用于同一个切入点时的执行顺序; +3. `OrderService` 是待切入方法。
- ### 1.2 依赖说明 -除了 spring 的基本依赖外,需要导入 aop 依赖包 +除了 Spring 的基本依赖外,需要导入 AOP 依赖包: ```xml - + org.springframework spring-aop @@ -41,9 +39,11 @@ -## 二、spring aop +## 二、Spring AOP -#### 2.1 创建待切入接口及其实现类 +### 2.1 准备工作 + +创建待切入的接口及其实现类: ```java public interface OrderService { @@ -67,18 +67,13 @@ public class OrderServiceImpl implements OrderService { return new Order(id, "new Product", new Date()); } } - ``` -#### 2.2 创建自定义切面类 +### 2.2 自定义切面类 -注:@Pointcut 的值可以是多个切面表达式的组合。 +使用 @Aspect 来定义切面类,@Pointcut 来定义切面表达式,它可以是多个切面表达式的组合: ```java -/** - * @author : heibaiying - * @description : 自定义切面 - */ @Aspect @Component //除了加上@Aspect 外 还需要声明为 spring 的组件 @Aspect 只是一个切面声明 public class CustomAdvice { @@ -132,13 +127,9 @@ public class CustomAdvice { ``` -#### 2.3 配置切面 +### 2.3 配置切面 ```java -/** - * @author : heibaiying - * @description : 开启切面配置 - */ @Configuration @ComponentScan("com.heibaiying.*") @EnableAspectJAutoProxy // 开启@Aspect 注解支持 等价于 @@ -146,7 +137,7 @@ public class AopConfig { } ``` -#### 2.4 测试切面 +### 2.4 测试切面 ```java @RunWith(SpringRunner.class) @@ -164,8 +155,8 @@ public class AopTest { } /** - * 多个切面作用于同一个切入点时,可以用@Order 指定切面的执行顺序 - * 优先级高的切面在切入方法前执行的通知 (before) 会优先执行,但是位于方法后执行的通知 (after,afterReturning) 反而会延后执行 + * 多个切面作用于同一个切入点时,可以用 @Order 指定切面的执行顺序 + * 优先级高的切面在切入方法前执行的通知 (如 before) 会优先执行,但是位于方法后执行的通知 (如 after,afterReturning) 反而会延后执行 */ @Test public void delete() { @@ -174,30 +165,30 @@ public class AopTest { } ``` -#### 2.5 切面执行顺序 +### 2.5 切面执行顺序 -- 多个切面作用于同一个切入点时,可以用@Order 指定切面的执行顺序 +- 多个切面作用于同一个切入点时,可以用 @Order 指定切面的执行顺序。 -- 优先级高的切面在切入方法前执行的通知 (before) 会优先执行,但是位于方法后执行的通知 (after,afterReturning) 反而会延后执行,类似于同心圆原理。 +- 优先级高的切面在切入方法前执行的通知 ( 如 before) 会优先执行,但是位于方法后执行的通知 ( 如 after,afterReturning ) 反而会延后执行,类似于同心圆原理:
-## 附: 关于切面表达式的说明 +## 三、切面表达式 切面表达式遵循以下格式: ```shell execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) - throws-pattern?) +throws-pattern?) ``` -- 除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的; -- `*`,它代表了匹配任意的返回类型; -- `()` 匹配了一个不接受任何参数的方法, 而 `(..)` 匹配了一个接受任意数量参数的方法(零或者更多)。 模式 `(*)` 匹配了一个接受一个任何类型的参数的方法。 模式 `(*,String)` 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是 String 类型。 +- 除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的。 +- `*` 代表了匹配任意的返回类型。 +- `()` 匹配一个不接受任何参数的方法, `(..)` 匹配一个接受任意数量参数的方法(零或者更多)。 `(*)` 匹配一个接受任意类型的参数的方法。 模式 `(*,String)` 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是 String 类型。 -下面给出一些常见切入点表达式的例子。 +下面为一些常见切入点表达式: - 任意公共方法的执行: @@ -205,25 +196,25 @@ execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-patter execution(public * *(..)) ``` -- 任何一个以“set”开始的方法的执行: +- 任何一个以 `set` 开头的方法的执行: ```java execution(* set*(..)) ``` -- `AccountService` 接口的任意方法的执行: +- `AccountService` 接口上任意方法的执行: ```java execution(* com.xyz.service.AccountService.*(..)) ``` -- 定义在 service 包里的任意方法的执行: +- 定义在 service 包里任意方法的执行: ```java execution(* com.xyz.service.*.*(..)) ``` -- 定义在 service 包或者子包里的任意方法的执行: +- 定义在 service 包或者子包里任意方法的执行: ```java execution(* com.xyz.service..*.*(..)) @@ -247,4 +238,4 @@ execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-patter this(com.xyz.service.AccountService) ``` -更多表达式可以参考官方文档:[Declaring a Pointcut](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#aop-pointcuts) +更多表达式可以参考官方文档:[Declaring a Pointcut](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#aop-pointcuts) \ No newline at end of file diff --git a/spring/spring-aop/README.md b/spring/spring-aop/README.md index 4159653..4bdcad6 100644 --- a/spring/spring-aop/README.md +++ b/spring/spring-aop/README.md @@ -1,34 +1,35 @@ -# spring AOP(xml配置方式) - -## 目录
+# spring AOP( XML 配置方式) + + +## ## 一、说明 ### 1.1 项目结构说明 -切面配置位于 resources 下的 aop.xml 文件,其中 CustomAdvice 是自定义切面类,OrderService 是待切入的方法。 +切面配置位于 resources 下的 `aop.xml` ,其中 CustomAdvice 是自定义切面类,OrderService 是待切入的方法。
- ### 1.2 依赖说明 -除了 spring 的基本依赖外,需要导入 aop 依赖包 +除了 Spring 的基本依赖外,还需需要导入 AOP 的依赖: ```xml - + org.springframework spring-aop @@ -38,9 +39,11 @@ -## 二、spring aop +## 二、Spring AOP -#### 2.1 创建待切入接口及其实现类 +### 2.1 准备工作 + +创建待切入的接口及其实现类: ```java public interface OrderService { @@ -64,20 +67,16 @@ public class OrderServiceImpl implements OrderService { return new Order(id, "new Product", new Date()); } } - ``` -#### 2.2 创建自定义切面类 +### 2.2 自定义切面 ```java -/** - * @author : heibaiying - * @description : 自定义切面 - */ public class CustomAdvice { - - //前置通知 + /** + *前置通知 + */ public void before(JoinPoint joinPoint) { //获取节点名称 String name = joinPoint.getSignature().getName(); @@ -85,12 +84,16 @@ public class CustomAdvice { System.out.println(name + "方法调用前:获取调用参数" + Arrays.toString(args)); } - //后置通知 (抛出异常后不会被执行) + /** + *后置通知 (抛出异常后不会被执行) + */ public void afterReturning(JoinPoint joinPoint, Object result) { System.out.println("后置返回通知结果" + result); } - //环绕通知 + /** + *环绕通知 + */ public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕通知-前"); //调用目标方法 @@ -99,14 +102,16 @@ public class CustomAdvice { return proceed; } - //异常通知 + /** + *异常通知 + */ public void afterException(JoinPoint joinPoint, Exception exception) { System.err.println("后置异常通知:" + exception); - } + }; - ; - - // 后置通知 总会执行 但是不能访问到返回值 + /** + *后置通知 总会执行 但是不能访问到返回值 + */ public void after(JoinPoint joinPoint) { System.out.println("后置通知"); } @@ -114,7 +119,7 @@ public class CustomAdvice { ``` -#### 2.3 配置切面 +### 2.3 配置切面 ```xml @@ -122,9 +127,9 @@ public class CustomAdvice { xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/aop - http://www.springframework.org/schema/aop/spring-aop.xsd"> + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/aop + http://www.springframework.org/schema/aop/spring-aop.xsd"> @@ -154,7 +159,7 @@ public class CustomAdvice { ``` -#### 2.4 测试切面 +### 2.4 测试切面 ```java @RunWith(SpringRunner.class) @@ -174,20 +179,20 @@ public class AopTest { -## 附: 关于切面表达式的说明 +## 三、切面表达式 切面表达式遵循以下格式: ```shell execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) - throws-pattern?) +throws-pattern?) ``` -- 除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的; -- `*`,它代表了匹配任意的返回类型; -- `()` 匹配了一个不接受任何参数的方法, 而 `(..)` 匹配了一个接受任意数量参数的方法(零或者更多)。 模式 `(*)` 匹配了一个接受一个任何类型的参数的方法。 模式 `(*,String)` 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是 String 类型。 +- 除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的。 +- `*` 代表了匹配任意的返回类型。 +- `()` 匹配一个不接受任何参数的方法, `(..)` 匹配一个接受任意数量参数的方法(零或者更多)。 `(*)` 匹配一个接受任意类型的参数的方法。 模式 `(*,String)` 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是 String 类型。 -下面给出一些常见切入点表达式的例子。 +下面为一些常见切入点表达式: - 任意公共方法的执行: @@ -195,25 +200,25 @@ execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-patter execution(public * *(..)) ``` -- 任何一个以“set”开始的方法的执行: +- 任何一个以 `set` 开头的方法的执行: ```java execution(* set*(..)) ``` -- `AccountService` 接口的任意方法的执行: +- `AccountService` 接口上任意方法的执行: ```java execution(* com.xyz.service.AccountService.*(..)) ``` -- 定义在 service 包里的任意方法的执行: +- 定义在 service 包里任意方法的执行: ```java execution(* com.xyz.service.*.*(..)) ``` -- 定义在 service 包或者子包里的任意方法的执行: +- 定义在 service 包或者子包里任意方法的执行: ```java execution(* com.xyz.service..*.*(..)) diff --git a/spring/spring-druid-mybatis-annotation/README.md b/spring/spring-druid-mybatis-annotation/README.md index b8ec2dc..8715021 100644 --- a/spring/spring-druid-mybatis-annotation/README.md +++ b/spring/spring-druid-mybatis-annotation/README.md @@ -1,23 +1,23 @@ -# spring +druid+ mybatis(注解方式) - -## 目录
-        1、创建maven工程,除了Spring基本依赖外,还需要导入mybatis和druid的相关依赖
-        2、新建 DispatcherServletInitializer.java继承自AbstractAnnotationConfigDispatcherServletInitializer,等价于我们在web.xml中配置的前端控制器
-        3、基于servlet 3.0的支持,可以采用注解的方式注册druid的servlet和filter
-        4、在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties
-        5、在新建数据库配置映射类DataSourceConfig.java
-        6、新建ServletConfig.java,进行数据库相关配置
-        7、新建mybtais 配置文件,按需要进行额外参数配置, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
-        8、新建查询接口及其对应的mapper文件
-        9、新建测试controller进行测试
-        10、druid 监控页面访问地址http://localhost:8080/druid/index.html
-## 正文
+# Spring +Druid+ Mybatis(注解方式) + ### 项目目录结构
+#### 1. 导入依赖 -#### 1、创建maven工程,除了Spring基本依赖外,还需要导入mybatis和druid的相关依赖 +创建 maven 工程,除了 Spring 的基本依赖外,还需要导入 Mybatis 和 Druid 的相关依赖: ```xml @@ -55,7 +55,9 @@
``` -#### 2、新建 DispatcherServletInitializer.java继承自AbstractAnnotationConfigDispatcherServletInitializer,等价于我们在web.xml中配置的前端控制器 +#### 2. 配置前端控制器 + +新建 DispatcherServletInitializer 继承自 AbstractAnnotationConfigDispatcherServletInitializer,等价于在 web.xml 方式中配置的前端控制器: ```java public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @@ -74,15 +76,11 @@ public class DispatcherServletInitializer extends AbstractAnnotationConfigDispat } ``` -#### 3、基于servlet 3.0的支持,可以采用注解的方式注册druid的servlet和filter +#### 3. 配置 Druid 监控 -​ 注:关于 servlet 更多注解支持可以查看[Servlet 规范文档](https://github.com/heibaiying/spring-samples-for-all/blob/master/referenced%20documents/Servlet3.1%E8%A7%84%E8%8C%83%EF%BC%88%E6%9C%80%E7%BB%88%E7%89%88%EF%BC%89.pdf) 中**8.1 小节 注解和可插拔性** +基于 servlet 3.0 的支持,可以采用注解的方式注册 druid 的 servlet 和 filter。关于 servlet 更多注解支持可以查看 [Servlet 规范文档](https://github.com/heibaiying/spring-samples-for-all/blob/master/referenced%20documents/Servlet3.1%E8%A7%84%E8%8C%83%EF%BC%88%E6%9C%80%E7%BB%88%E7%89%88%EF%BC%89.pdf) 中的 **8.1 小节 注解和可插拔性** ```java -/** - * @author : heibaiying - * @description : 配置监控页面用户名密码 - */ @WebServlet(urlPatterns = "/druid/*", initParams={ @WebInitParam(name="resetEnable",value="true"), @@ -95,11 +93,6 @@ public class DruidStatViewServlet extends StatViewServlet { ``` ```java - -/** - * @author : heibaiying - * @description : WebStatFilter 用于采集 web-jdbc 关联监控的数据 - */ @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源 @@ -108,10 +101,11 @@ public class DruidStatViewServlet extends StatViewServlet { public class DruidStatFilter extends WebStatFilter { } - ``` -#### 4、在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties +#### 4. 数据库配置 + +在 resources 文件夹下新建数据库配置文件及其映射类: ```properties # mysql 数据库配置 @@ -129,8 +123,6 @@ oracle.username=用户名 oracle.password=密码 ``` -#### 5、在新建数据库配置映射类DataSourceConfig.java - ```java @Configuration @PropertySource(value = "classpath:mysql.properties") @@ -147,15 +139,13 @@ public class DataSourceConfig { private String password; } - ``` -#### 6、新建ServletConfig.java,进行数据库相关配置 +#### 5. Druid 连接池配置 + +新建 ServletConfig,进行数据库相关配置: ```java -/** - * @author : heibaiying - */ @Configuration @EnableTransactionManagement // 开启声明式事务处理 等价于 xml 中 @EnableWebMvc @@ -249,12 +239,12 @@ public class ServletConfig implements WebMvcConfigurer { manager.setDataSource(dataSource); return manager; } - } - ``` -#### 7、新建mybtais 配置文件,按需要进行额外参数配置, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) +#### 6. MyBatis 配置 + +新建 mybtais 配置文件,按照需求配置额外参数, 更多 settings 配置项可以参考 [官方文档]( ```xml @@ -272,12 +262,11 @@ public class ServletConfig implements WebMvcConfigurer { - - - ``` -#### 8、新建查询接口及其对应的mapper文件 +#### 7. 数据查询 + +新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表: ```java public interface MysqlDao { @@ -324,7 +313,9 @@ public interface OracleDao { ``` -#### 9、新建测试controller进行测试 +#### 8. 测试查询 + +新建测试类进行测试: ```java @RestController @@ -338,7 +329,6 @@ public class MysqlController { return mysqlDao.queryById(id).get(0).toString(); } } - ``` ```java @@ -353,10 +343,11 @@ public class OracleController { return oracleDao.queryById(id).get(0).toString(); } } - ``` -#### 10、druid 监控页面访问地址http://localhost:8080/druid/index.html +#### 9. Druid 监控台 + +Druid Web 页面访问地址为:http://localhost:8080/druid/index.html,可以登录后查看数据库相关监控数据: ![druid 控制台](https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/druid%E6%8E%A7%E5%88%B6%E5%8F%B0.png) diff --git a/spring/spring-druid-mybatis/README.md b/spring/spring-druid-mybatis/README.md index 4f807f9..3d4beaa 100644 --- a/spring/spring-druid-mybatis/README.md +++ b/spring/spring-druid-mybatis/README.md @@ -1,21 +1,24 @@ -# spring +druid+ mybatis(xml配置方式) - -## 目录
-        1、创建maven工程,除了Spring基本依赖外,还需要导入mybatis和druid的相关依赖
-        2、在web.xml 中配置spring前端控制器、druid监控台servlet和filter
-        3、在resources文件夹下新建数据库配置文件jdbc.properties
-        4、在resources文件夹下创建springApplication.xml 配置文件和druid.xml配置文件
-        5、新建mybtais 配置文件,按需要进行额外配置,更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
-        6、新建查询接口及其对应的mapper文件
-        7、新建测试controller进行测试
-        8、druid 监控页面访问地址http://localhost:8080/druid/index.html
-## 正文
+# Spring +Druid+ Mybatis(XML 配置方式) + ### 项目目录结构
-#### 1、创建maven工程,除了Spring基本依赖外,还需要导入mybatis和druid的相关依赖 + +#### 1. 导入依赖 + +创建 maven 工程,除了 Spring 的基本依赖外,还需要导入 Mybatis 和 Druid 的相关依赖: ```xml @@ -53,7 +56,9 @@
``` -#### 2、在web.xml 中配置spring前端控制器、druid监控台servlet和filter +#### 2. web.xml 配置 + +在 `web.xml` 中配置 Spring 的前端控制器以及 Druid 的 Web 监控台,用于获取数据库的相关监控信息: ```xml @@ -118,11 +123,12 @@ /* - ``` -#### 3、在resources文件夹下新建数据库配置文件jdbc.properties +#### 3. 数据库配置 + +在 resources 文件夹下新建数据库配置文件 `jdbc.properties`: ```properties # mysql 数据库配置 @@ -136,7 +142,9 @@ oracle.username=用户名 oracle.password=密码 ``` -#### 4、在resources文件夹下创建springApplication.xml 配置文件和druid.xml配置文件 +#### 4. Druid 连接池配置 + +在 resources 文件夹下创建 `springApplication.xml` 配置文件和 `druid.xml` 配置文件: ```xml @@ -145,7 +153,7 @@ oracle.password=密码 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> @@ -185,7 +193,6 @@ oracle.password=密码 - ``` @@ -195,8 +202,7 @@ oracle.password=密码 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> - + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> @@ -245,11 +251,12 @@ oracle.password=密码 - ``` -#### 5、新建mybtais 配置文件,按需要进行额外配置,更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) +#### 5. MyBatis 配置 + +新建 mybtais 配置文件,按照需求配置额外参数, 更多 settings 配置项可以参考 [官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) ```xml @@ -267,12 +274,11 @@ oracle.password=密码 - - - ``` -#### 6、新建查询接口及其对应的mapper文件 +#### 6. 数据查询 + +新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表: ```java public interface MysqlDao { @@ -319,25 +325,11 @@ public interface OracleDao { ``` -#### 7、新建测试controller进行测试 +#### 7. 测试查询 + +新建测试类进行测试: ```java -package com.heibaiying.controller; - -import com.heibaiying.bean.Relation; -import com.heibaiying.dao.MysqlDao; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; - -/** - * @author : heibaiying - * @description : - */ - @RestController public class MysqlController { @@ -349,23 +341,9 @@ public class MysqlController { return mysqlDao.queryById(id).get(0).toString(); } } - ``` ```java -package com.heibaiying.controller; - -import com.heibaiying.dao.OracleDao; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author : heibaiying - * @description : - */ - @RestController public class OracleController { @@ -377,9 +355,10 @@ public class OracleController { return oracleDao.queryById(id).get(0).toString(); } } - ``` -#### 8、druid 监控页面访问地址http://localhost:8080/druid/index.html +#### 8. Druid 监控台 + +Druid Web 页面访问地址为:http://localhost:8080/druid/index.html,可以登录后查看数据库相关监控数据: ![druid 控制台](https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/druid%E6%8E%A7%E5%88%B6%E5%8F%B0.png) diff --git a/spring/spring-dubbo-annotation/README.md b/spring/spring-dubbo-annotation/README.md index 71c1ef1..e586953 100644 --- a/spring/spring-dubbo-annotation/README.md +++ b/spring/spring-dubbo-annotation/README.md @@ -1,39 +1,34 @@ -# spring 整合 dubbo(注解方式) +# Spring 整合 Dubbo(注解方式) -## 目录
-一、 项目结构说明
+ +## 一、 项目结构 -## 一、 项目结构说明 +按照 Dubbo 官方文档推荐的服务最佳化方案,建议将服务接口、服务模型、服务异常等均放在单独的 API 包中,所以项目采用 maven 多模块的构建方式,在 spring-dubbo 下构建三个子模块: -1.1 按照 dubbo 文档推荐的服务最佳实践,建议将服务接口、服务模型、服务异常等均放在 API 包中,所以项目采用 maven 多模块的构建方式,在 spring-dubbo-annotation 下构建三个子模块: +- **dubbo-common**:公共模块,用于存放公共的接口和 bean,被 dubbo-provider 和 dubbo-provider 所引用; +- **dubbo-provider** :服务的提供者,提供商品的查询服务; +- **dubbo-provider** :是服务的消费者,调用 provider 提供的查询服务。 -1. dubbo-ano-common 是公共模块,用于存放公共的接口和 bean,被 dubbo-ano-provider 和 dubbo-ano-provider 在 pom.xml 中引用; -2. dubbo-ano-provider 是服务的提供者,提供商品的查询服务; -3. dubbo-ano-provider 是服务的消费者,调用 provider 提供的查询服务。 - -1.2 本项目 dubbo 的搭建采用 zookeeper 作为注册中心, 关于 zookeeper 的安装和基本操作可以参见我的手记[Zookeeper 基础命令与 Java 客户端](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/ZooKeeper/ZooKeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E4%B8%8EJava%E5%AE%A2%E6%88%B7%E7%AB%AF.md) +另外,本项目 Dubbo 的搭建采用 ZooKeeper 作为注册中心。
- - ## 二、项目依赖 -**在父工程的项目中统一导入依赖 dubbo 依赖的的 jar 包** - -这里需要注意的是 ZooKeeper 3.5.x 和 ZooKeeper 3.4.x 是存在不兼容的情况 详见官网解释[ZooKeeper Version Compatibility](https://curator.apache.org/zk-compatibility.html), zookeeper 3.5 目前是 beta 版本,所以 zookeeper 我选择的版本是 zookeeper-3.4.9 作为服务端。但默认情况下 curator-framework 自动引用的最新的 3.5 的版本客户端,会出现 KeeperException$UnimplementedException 异常 +在父工程的项目中统一导入依赖 Dubbo 的依赖: ```xml @@ -59,20 +54,18 @@ ``` +上面之所以要排除 curator-framework 中的 zookeeper,然后再次进行引入,是因为默认情况下 curator-framework 自动引用的最新的 3.5.x 的 zookeeper,但我本地安装是 3.4.x 的 zookeeper (因为我安装时候 zookeeper 3.5 还是 beta 版本),此时会出现 KeeperException$UnimplementedException 异常。因为 ZooKeeper 3.5.x 和 ZooKeeper 3.4.x 存在不兼容的情况,详见官方说明 [ZooKeeper Version Compatibility](https://curator.apache.org/zk-compatibility.html) 。 - -## 三、公共模块(dubbo-ano-common) +## 三、公共模块 - api 下为公共的调用接口; - bean 下为公共的实体类。
- -## 四、 服务提供者(dubbo-ano-provider) +## 四、 服务提供者
- -#### 4.1 提供方配置 +### 4.1 提供者配置 ```java @Configuration @@ -112,26 +105,13 @@ public class DubboConfiguration { } ``` -#### 4.2 使用注解@Service暴露服务 +### 4.2 暴露服务 -需要注意的是这里的@Service 注解不是 spring 的注解,而是 dubbo 的注解 com.alibaba.dubbo.config.annotation.Service +使用注解 @Service 暴露服务,需要注意的是这里的 @Service 注解不是 spring 的注解,而是 dubbo 的注解,完整路径为:com.alibaba.dubbo.config.annotation.Service : ```java -package com.heibaiying.service; - import com.alibaba.dubbo.config.annotation.Service; -import com.heibaiying.api.IProductService; -import com.heibaiying.bean.Product; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * @author : heibaiying - * @description : 产品提供接口实现类 - */ @Service(timeout = 5000) public class ProductService implements IProductService { @@ -160,11 +140,10 @@ public class ProductService implements IProductService { -## 五、服务消费者(dubbo-ano-consumer) +## 五、服务消费者
- -#### 1.消费方的配置 +### 5.1 消费者配置 ```java @Configuration @@ -206,20 +185,12 @@ public class DubboConfiguration { } ``` -#### 2.使用注解@Reference引用远程服务 +### 5.2 调用远程服务 + +使用注解 @Reference 引用远程服务: ```java -package com.heibaiying.controller; - import com.alibaba.dubbo.config.annotation.Reference; -import com.heibaiying.api.IProductService; -import com.heibaiying.bean.Product; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; - -import java.util.List; @Controller @RequestMapping("sell") @@ -246,73 +217,17 @@ public class SellController { ``` -## 六、项目构建的说明 +## 六、项目构建 -因为在项目中,consumer 和 provider 模块均依赖公共模块,所以在构建 consumer 和 provider 项目前需要将 common 模块安装到本地仓库,**依次**对**父工程**和**common 模块**执行: +在项目中,consumer 和 provider 模块均依赖公共模块,所以在构建 consumer 和 provider 模块前需要将 common 模块安装到本地仓库,依次对 父工程 和 common 模块执行以下命令: ```shell mvn install -Dmaven.test.skip = true ``` -consumer 中 pom.xml 如下 +## 七、Dubbo 控制台 -```xml - - - - spring-dubbo-annotation - com.heibaiying - 1.0-SNAPSHOT - - 4.0.0 - - dubbo-ano-consumer - - - - com.heibaiying - dubbo-ano-common - 1.0-SNAPSHOT - compile - - - - -``` - -provider 中 pom.xml 如下 - -```xml - - - - spring-dubbo-annotation - com.heibaiying - 1.0-SNAPSHOT - - 4.0.0 - - dubbo-ano-provider - - - - com.heibaiying - dubbo-ano-common - 1.0-SNAPSHOT - compile - - - - -``` - -## 七、关于dubbo新版本管理控制台的安装说明 - -安装: +Dubbo 新版本管理控制台的安装步骤如下: ```sh git clone https://github.com/apache/incubator-dubbo-ops.git /var/tmp/dubbo-ops @@ -322,21 +237,22 @@ mvn clean package 配置: -```sh -配置文件为: +```properties +# 配置文件为: dubbo-admin-backend/src/main/resources/application.properties -主要的配置有 默认的配置就是 127.0.0.1:2181: + +# 可以在其中修改zookeeper的地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 ``` -启动: +启动: ```sh mvn --projects dubbo-admin-backend spring-boot:run ``` -访问: +访问: -``` +```shell http://127.0.0.1:8080 ``` diff --git a/spring/spring-dubbo/README.md b/spring/spring-dubbo/README.md index f1ed719..071e97f 100644 --- a/spring/spring-dubbo/README.md +++ b/spring/spring-dubbo/README.md @@ -1,39 +1,35 @@ -# spring 整合 dubbo(xml配置方式) +# Spring 整合 Dubbo(XML 配置方式) -## 目录
-一、 项目结构说明
+ +## 一、 项目结构 -## 一、 项目结构说明 +按照 Dubbo 官方文档推荐的服务最佳化方案,建议将服务接口、服务模型、服务异常等均放在单独的 API 包中,所以项目采用 maven 多模块的构建方式,在 spring-dubbo 下构建三个子模块: -1.1 按照 dubbo 文档推荐的服务最佳实践,建议将服务接口、服务模型、服务异常等均放在 API 包中,所以项目采用 maven 多模块的构建方式,在 spring-dubbo 下构建三个子模块: +- **dubbo-common**:公共模块,用于存放公共的接口和 bean,被 dubbo-provider 和 dubbo-provider 所引用; +- **dubbo-provider** :服务的提供者,提供商品的查询服务; +- **dubbo-provider** :是服务的消费者,调用 provider 提供的查询服务。 -1. dubbo-common 是公共模块,用于存放公共的接口和 bean,被 dubbo-provider 和 dubbo-provider 在 pom.xml 中引用; -2. dubbo-provider 是服务的提供者,提供商品的查询服务; -3. dubbo-provider 是服务的消费者,调用 provider 提供的查询服务。 - -1.2 本项目 dubbo 的搭建采用 zookeeper 作为注册中心, 关于 zookeeper 的安装和基本操作可以参见我的手记[Zookeeper 基础命令与 Java 客户端](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/ZooKeeper/ZooKeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E4%B8%8EJava%E5%AE%A2%E6%88%B7%E7%AB%AF.md) +另外,本项目 Dubbo 的搭建采用 ZooKeeper 作为注册中心。
- ## 二、项目依赖 -**在父工程的项目中统一导入依赖 dubbo 依赖的的 jar 包** - -这里需要注意的是 ZooKeeper 3.5.x 和 ZooKeeper 3.4.x 是存在不兼容的情况 详见官网解释[ZooKeeper Version Compatibility](https://curator.apache.org/zk-compatibility.html), zookeeper 3.5 目前是 beta 版本,所以 zookeeper 我选择的版本是 zookeeper-3.4.9 作为服务端。但默认情况下 curator-framework 自动引用的最新的 3.5 的版本客户端,会出现 KeeperException$UnimplementedException 异常 +在父工程的项目中统一导入依赖 Dubbo 的依赖: ```xml @@ -59,28 +55,22 @@ ``` +上面之所以要排除 curator-framework 中的 zookeeper,然后再次进行引入,是因为默认情况下 curator-framework 自动引用的最新的 3.5.x 的 zookeeper,但我本地安装是 3.4.x 的 zookeeper (因为我安装时候 zookeeper 3.5 还是 beta 版本),此时会出现 KeeperException$UnimplementedException 异常。因为 ZooKeeper 3.5.x 和 ZooKeeper 3.4.x 存在不兼容的情况,详见官方说明 [ZooKeeper Version Compatibility](https://curator.apache.org/zk-compatibility.html) 。 - -## 三、公共模块(dubbo-common) +## 三、公共模块 - api 下为公共的调用接口; - bean 下为公共的实体类。
- -## 四、 服务提供者(dubbo-provider) +## 四、 服务提供者
+### 4.1 开发服务 -#### 4.1 productService是服务的提供者( 商品数据用模拟数据展示) - -注:这里实现的接口 IProductService 来源于公共模块 +productService 是服务的提供者,其实现的接口 IProductService 来源于公共模块,这里商品数据用模拟数据展示: ```java -/** - * @author : heibaiying - * @description : 产品提供接口实现类 - */ @Service public class ProductService implements IProductService { @@ -108,7 +98,9 @@ public class ProductService implements IProductService { } ``` -#### 4.2 在dubbo.xml暴露服务 +### 4.2 暴露服务 + +在 `dubbo.xml` 中暴露服务: ```xml @@ -137,11 +129,12 @@ public class ProductService implements IProductService { ``` -## 五、服务消费者(dubbo-consumer) +## 五、服务消费者
+### 5.1 调用服务 -#### 1.在dubbo.xml调用远程的服务 +在 `dubbo.xml` 中调用远程的服务: ```xml @@ -172,7 +165,7 @@ public class ProductService implements IProductService { ``` -#### 2.消费服务 +### 5.2 消费服务 ```java @Controller @@ -198,73 +191,17 @@ public class SellController { } ``` -## 六、项目构建的说明 +## 六、项目构建 -因为在项目中,consumer 和 provider 模块均依赖公共模块,所以在构建 consumer 和 provider 项目前需要将 common 模块安装到本地仓库,**依次**对**父工程**和**common 模块**执行: +在项目中,consumer 和 provider 模块均依赖公共模块,所以在构建 consumer 和 provider 模块前需要将 common 模块安装到本地仓库,依次对 父工程 和 common 模块执行以下命令: ```shell mvn install -Dmaven.test.skip = true ``` -consumer 中 pom.xml 如下 +## 七、Dubbo 控制台 -```xml - - - - spring-dubbo - com.heibaiying - 1.0-SNAPSHOT - - 4.0.0 - - dubbo-consumer - - - - com.heibaiying - dubbo-common - 1.0-SNAPSHOT - compile - - - - -``` - -provider 中 pom.xml 如下 - -```xml - - - - spring-dubbo - com.heibaiying - 1.0-SNAPSHOT - - 4.0.0 - - dubbo-provider - - - - com.heibaiying - dubbo-common - 1.0-SNAPSHOT - compile - - - - -``` - -## 七、关于dubbo新版本管理控制台的安装说明 - -安装: +Dubbo 新版本管理控制台的安装步骤如下: ```sh git clone https://github.com/apache/incubator-dubbo-ops.git /var/tmp/dubbo-ops @@ -274,21 +211,22 @@ mvn clean package 配置: -```sh -配置文件为: +```properties +# 配置文件为: dubbo-admin-backend/src/main/resources/application.properties -主要的配置有 默认的配置就是 127.0.0.1:2181: + +# 可以在其中修改zookeeper的地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 ``` -启动: +启动: ```sh mvn --projects dubbo-admin-backend spring-boot:run ``` -访问: +访问: -``` +```shell http://127.0.0.1:8080 ``` diff --git a/spring/spring-email-annotation/README.md b/spring/spring-email-annotation/README.md index 55f1edf..aef0ef7 100644 --- a/spring/spring-email-annotation/README.md +++ b/spring/spring-email-annotation/README.md @@ -1,33 +1,33 @@ -# spring 邮件发送(xml配置方式) +# Spring 邮件发送(注解方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring email
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 +- 邮件的发送配置类为 com.heibaiying.config 下 EmailConfig; -1. 邮件发送配置类为 com.heibaiying.config 下 EmailConfig.java; -2. 简单邮件发送、附件邮件发送、内嵌资源邮件发送、模板邮件发送的方法封装在 SpringMail 类中; -3. 项目以单元测试的方法进行测试,测试类为 SendEmail。 +- 简单邮件发送、附件邮件发送、内嵌资源邮件发送、模板邮件发送的方法封装在 SpringMail 类中; +- 项目以单元测试的方法进行测试,测试类为 SendEmail。 +
+### 1.2 基本依赖 -### 1.2 依赖说明 - -除了 spring 的基本依赖外,需要导入邮件发送的支持包 spring-context-support +除了 Spring 的基本依赖外,需要导入邮件发送的支持包 spring-context-support: ```xml @@ -47,16 +47,11 @@ -## 二、spring email +## 二、Spring Email -#### 2.1 邮件发送配置 +### 2.1 邮件发送配置 ```java -/** - * @author : heibaiying - * @description : 邮件发送配置类 - */ - @Configuration @ComponentScan(value = "com.heibaiying.email") public class EmailConfig { @@ -87,11 +82,10 @@ public class EmailConfig { ``` -#### 2.2 新建邮件发送基本类 +### 2.2 发送邮件 ```java /** - * @author : heibaiying * @description : 邮件发送基本类 */ @Component @@ -218,18 +212,12 @@ public class SpringMail { System.err.println("发送邮件失败" + ex.getMessage()); } } - } - ``` -**关于模板邮件的说明:** +- **关于模板邮件的说明:** -- 模板引擎最主要的作用是,在对邮件格式有要求的时候,采用拼接字符串不够直观,所以采用模板引擎; - -- 这里我们使用的 beetl 模板引擎,原因是其性能优异,官网是介绍其性能 6 倍与 freemaker,并有完善的文档支持。当然大家也可以换成任何其他的模板引擎(freemarker,thymeleaf) - - 一个简单的模板 template.html 如下: + 通常邮件都有规范的格式要求,因此 Spring 支持使用任意模板引擎来配置模板。这里我们使用的 beetl 模板引擎,其性能比较优异,官网是介绍其性能 6 倍于 freemaker,当然也可以换成其他模板引擎( 如 freemarker,thymeleaf)。示例如下: ```html @@ -244,15 +232,9 @@ public class SpringMail { ``` - - -#### 2.3 邮件发送的测试 +### 2.3 单元测试 ```java -/** - * @author : heibaiying - * @description : 发送邮件测试类 - */ @RunWith(SpringRunner.class) @ContextConfiguration(classes = EmailConfig.class) public class SendEmail { @@ -296,3 +278,4 @@ public class SendEmail { } } ``` + diff --git a/spring/spring-email/README.md b/spring/spring-email/README.md index 0585216..ccd3ca4 100644 --- a/spring/spring-email/README.md +++ b/spring/spring-email/README.md @@ -1,43 +1,43 @@ -# spring 邮件发送(xml配置方式) +# Spring 邮件发送(XML 配置方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring email
-        2.1 邮件发送配置
-        2.2 新建邮件发送基本类
-        2.3 邮件发送的测试
-## 正文
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 +- 邮件发送的配置文件为 `springApplication.xml`; -1. 邮件发送配置文件为 springApplication.xml; -2. 简单邮件发送、附件邮件发送、内嵌资源邮件发送、模板邮件发送的方法封装在 SpringMail 类中; -3. 项目以单元测试的方法进行测试,测试类为 SendEmail。 +- 简单邮件发送、附件邮件发送、内嵌资源邮件发送、模板邮件发送的方法封装在 SpringMail 类中; +- 项目以单元测试的方法进行测试,测试类为 SendEmail。 +
+### 1.2 基本依赖 -### 1.2 依赖说明 - -除了 spring 的基本依赖外,需要导入邮件发送的支持包 spring-context-support +除了 Spring 的基本依赖外,需要导入邮件发送的支持包 spring-context-support: ```xml - + org.springframework spring-context-support ${spring-base-version} - - + + com.ibeetl beetl @@ -47,9 +47,9 @@ -## 二、spring email +## 二、Spring Email -#### 2.1 邮件发送配置 +### 2.1 基本配置 ```xml @@ -86,11 +86,10 @@ ``` -#### 2.2 新建邮件发送基本类 +### 2.2 发送邮件 ```java /** - * @author : heibaiying * @description : 邮件发送基本类 */ @Component @@ -217,18 +216,12 @@ public class SpringMail { System.err.println("发送邮件失败" + ex.getMessage()); } } - } - ``` **关于模板邮件的说明:** -- 模板引擎最主要的作用是,在对邮件格式有要求的时候,采用拼接字符串不够直观,所以采用模板引擎; - -- 这里我们使用的 beetl 模板引擎,原因是其性能优异,官网是介绍其性能 6 倍与 freemaker,并有完善的文档支持。当然大家也可以换成任何其他的模板引擎(freemarker,thymeleaf) - - 一个简单的模板 template.html 如下: +通常邮件都有规范的格式要求,因此 Spring 支持使用任意模板引擎来配置模板。这里我们使用的 beetl 模板引擎,其性能比较优异,官网是介绍其性能 6 倍于 freemaker,当然也可以换成其他模板引擎( 如 freemarker,thymeleaf)。示例如下: ```html @@ -243,15 +236,9 @@ public class SpringMail { ``` - - -#### 2.3 邮件发送的测试 +### 2.3 单元测试 ```java -/** - * @author : heibaiying - * @description : 发送邮件测试类 - */ @RunWith(SpringRunner.class) @ContextConfiguration({"classpath:springApplication.xml"}) public class SendEmail { diff --git a/spring/spring-jdbc-annotation/README.md b/spring/spring-jdbc-annotation/README.md index 65d4f22..91b1186 100644 --- a/spring/spring-jdbc-annotation/README.md +++ b/spring/spring-jdbc-annotation/README.md @@ -1,27 +1,27 @@ -# spring 整合 jdbc template(注解方式) - -## 目录
-1.说明
-        1.2 项目依赖
-二、spring 整合 jdbc template
-        2.1 在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties及其映射类
-        2.2 新建数据库配置类DatabaseConfig.java
-        2.3 新建查询接口及其实现类
-        2.4 新建测试类进行测试
-## 正文
+# Spring 整合 Jdbc Template(注解方式) -## 1.说明 + -#### 1.1 项目目录结构 +## 一、项目说明 -1. 数据源配置位于 config 目录下的 DatabaseConfig.java 和 DataSourceConfig.java -2. 项目以单元测试的方法进行测试 +#### 1.1 项目结构 + +1. 数据源配置位于 config 目录下的 DatabaseConfig 和 DataSourceConfig; +2. 项目以单元测试的方法进行测试。
- #### 1.2 项目依赖 ```xml @@ -44,9 +44,13 @@ ``` -## 二、spring 整合 jdbc template -#### 2.1 在resources文件夹下新建数据库配置文件mysql.properties、oracle.properties及其映射类 + +## 二、整合 JDBC Template + +#### 2.1 数据库配置 + +在 resources 文件夹下新建数据库配置文件 mysql.properties、oracle.properties 及其映射的实体类: ```properties # mysql 数据库配置 @@ -80,10 +84,11 @@ public class DataSourceConfig { private String password; } - ``` -#### 2.2 新建数据库配置类DatabaseConfig.java +#### 2.2 配置数据源 + +基于注解方式配置数据源: ```java @Configuration @@ -128,10 +133,11 @@ public class DatabaseConfig { } } - ``` -#### 2.3 新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表 +#### 2.3 数据查询 + +新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表: ```java @Repository @@ -158,11 +164,8 @@ public class MysqlDaoImpl implements MysqlDao { return relations; } } - ``` - - ```java @Repository public class OracleDaoImpl implements OracleDao { @@ -170,10 +173,6 @@ public class OracleDaoImpl implements OracleDao { @Autowired private JdbcTemplate jdbcTemplate; - /** - * 更多 JDBC 的使用可以参考官方文档 - * @see JdbcTemplate - */ public List get() { List flows = jdbcTemplate.query("select * from APEX_030200.WWV_FLOW_CALS where ID = ? ", new Object[]{217584603977429772L}, new RowMapper() { @@ -189,10 +188,11 @@ public class OracleDaoImpl implements OracleDao { return flows; } } - ``` -#### 2.4 新建测试类进行测试 +#### 2.4 测试查询 + +新建测试类进行测试: ```java @RunWith(SpringRunner.class) @@ -234,7 +234,5 @@ public class OracleDaoTest { } } } - - ``` diff --git a/spring/spring-jdbc/README.md b/spring/spring-jdbc/README.md index 54a830f..bad09c8 100644 --- a/spring/spring-jdbc/README.md +++ b/spring/spring-jdbc/README.md @@ -1,23 +1,22 @@ -# spring 整合 jdbc template(xml配置方式) +# Spring 整合 JDBC Template(XML 配置方式) -## 目录
+ + ## 一、说明 #### 1.1 项目结构
- #### 1.2 项目依赖 ```xml @@ -42,9 +41,11 @@ -## 二、 spring 整合 jdbc template +## 二、整合 JDBC Template -#### 1、在resources文件夹下新建数据库配置文件jdbc.properties +#### 2.1 数据库配置 + +在 resources 文件夹下新建数据库配置文件 `jdbc.properties`,内容如下: ```properties # mysql 数据库配置 @@ -60,7 +61,9 @@ oracle.username=用户名 oracle.password=密码 ``` -#### 2、配置Jdbc数据源并定义事务管理器 +#### 2.2 配置数据源 + +在配置文件中配置 JDBC 数据源并定义事务管理器: ```xml @@ -68,7 +71,7 @@ oracle.password=密码 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> @@ -108,7 +111,9 @@ oracle.password=密码 ``` -#### 3、新建查询接口及其实现类,这里我查询的表是mysql和oracle中的字典表 +#### 2.3 数据查询 + +新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表: ```java @Repository @@ -145,10 +150,6 @@ public class OracleDaoImpl implements OracleDao { @Autowired private JdbcTemplate jdbcTemplate; - /** - * 更多 JDBC 的使用可以参考官方文档 - * @see JdbcTemplate - */ public List get() { List flows = jdbcTemplate.query("select * from APEX_030200.WWV_FLOW_CALS where ID = ? ", new Object[]{217584603977429772L}, new RowMapper() { @@ -166,7 +167,9 @@ public class OracleDaoImpl implements OracleDao { } ``` -#### 4、新建测试类进行测试 +#### 2.4 测试查询 + +新建测试类进行测试: ```java @RunWith(SpringRunner.class) @@ -186,7 +189,6 @@ public class MysqlDaoTest { } } } - ``` ```java diff --git a/spring/spring-memcached-annotation/README.md b/spring/spring-memcached-annotation/README.md index 8133a19..abcb5d5 100644 --- a/spring/spring-memcached-annotation/README.md +++ b/spring/spring-memcached-annotation/README.md @@ -1,34 +1,32 @@ -# spring 整合 mecached(注解方式) +# Spring 整合 Mecached(注解方式) -## 目录
-一、说明
-    1.1 XMemcached客户端说明
-    1.2 项目结构说明
-    1.3 依赖说明
-二、spring 整合 memcached
-        2.1 单机配置
-        2.2 集群配置
-        2.3 存储基本类型测试用例
-        2.5 存储实体对象测试用例
-附:memcached 基本命令
-## 正文
+ +## 一、项目说明 -## 一、说明 +### 1.1 XMemcached -### 1.1 XMemcached客户端说明 +XMemcached 是基于 Java NIO 实现的 Memcached 的高性能客户端,支持完整的 Memcached 协议,支持客户端分布并且提供了一致性哈希 (consistent hash) 算法的实现。 -XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 memcached 协议,支持客户端分布并且提供了一致性哈希 (consistent hash) 算法的实现。 +### 1.2 项目结构 -### 1.2 项目结构说明 - -1. memcached 的整合配置位于 com.heibaiying.config 文件夹下。 +Memcached 的整合配置位于 com.heibaiying.config 文件夹下:
+### 1.3 相关依赖 -### 1.3 依赖说明 - -除了 spring 的基本依赖外,需要导入 xmemcached 依赖包 +除了 Spring 的基本依赖外,需要导入 xmemcached 依赖包: ```xml @@ -41,7 +39,7 @@ XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 -## 二、spring 整合 memcached +## 二、整合 XMemcached #### 2.1 单机配置 @@ -91,13 +89,9 @@ public MemcachedClient memcachedClientForCluster() { #### 2.3 存储基本类型测试用例 -xmemcached 单机版本和集群版本注入的实例是相同的; +XMemcached 单机版和集群版注入的实例是完全相同的: ```java -/** - * @author : heibaiying - * @description : Memcached 操作基本对象 - */ @RunWith(SpringRunner.class) @ContextConfiguration(classes = {MemcacheConfig.class}) public class MemSamples { @@ -120,10 +114,6 @@ public class MemSamples { #### 2.5 存储实体对象测试用例 ```java -/** - * @author : heibaiying - * @description :Memcached 序列化与反序列化 - */ @RunWith(SpringRunner.class) @ContextConfiguration(classes = {MemcacheConfig.class}) public class MemObjectSamples { @@ -141,12 +131,11 @@ public class MemObjectSamples { Assert.assertNull(programmer); } } - ``` -## 附:memcached 基本命令 +## 附:Memcached 基本命令 | 命令 | 格式 | 说明 | | --------------- | -------------------------------------------------- | ------------------------------------- | diff --git a/spring/spring-memcached/README.md b/spring/spring-memcached/README.md index 8a24866..db48855 100644 --- a/spring/spring-memcached/README.md +++ b/spring/spring-memcached/README.md @@ -1,32 +1,30 @@ -# spring 整合 mecached(xml配置方式) +# Spring 整合 Mecached(XML配置方式) -## 目录
-一、说明
-    1.1 XMemcached客户端说明
-    1.2 项目结构说明
-    1.3 依赖说明
-二、spring 整合 memcached
+ +## 一、项目说明 -## 一、说明 +### 1.1 XMemcached -### 1.1 XMemcached客户端说明 +XMemcached 是基于 Java NIO 实现的 Memcached 的高性能客户端,支持完整的 Memcached 协议,支持客户端分布并且提供了一致性哈希 (consistent hash) 算法的实现。 -XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 memcached 协议,支持客户端分布并且提供了一致性哈希 (consistent hash) 算法的实现。 +### 1.2 项目结构 -### 1.2 项目结构说明 - -1. memcached 的整合配置位于 resources 下的 memcached 文件夹下,其中集群配置用 cluster 开头。所有配置按照需要在 springApplication.xml 用 import 导入。 -2. 实体类 Programmer.java 用于测试 memcached 序列化与反序列化 +- Memcached 的整合配置位于 resources 下的 memcached 文件夹下,其中集群配置以 cluster 开头。所有配置按需在 `springApplication.xml` 中用 import 标签导入。 +- 实体类 Programmer 用于测试 Memcached 的序列化与反序列化。
- **springapplication.xml 文件:** ```xml @@ -44,9 +42,9 @@ XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 ``` -### 1.3 依赖说明 +### 1.3 相关依赖 -除了 spring 的基本依赖外,需要导入 xmemcached 依赖包 +除了 Spring 的基本依赖外,需要导入 xmemcached 依赖包: ```xml @@ -59,9 +57,9 @@ XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 -## 二、spring 整合 memcached +## 二、整合 XMemcached -#### 2.1 单机配置 +### 2.1 单机配置 ```xml @@ -79,7 +77,7 @@ XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 ``` -#### 2.2 集群配置 +### 2.2 集群配置 ```xml @@ -129,15 +127,11 @@ XMemcached 是基于 java nio 的 memcached 高性能客户端,支持完整的 ``` -#### 2.3 存储基本类型测试用例 +### 2.3 存储基本类型测试用例 -xmemcached 单机版本和集群版本注入的实例是相同的; +XMemcached 单机版和集群版注入的实例是完全相同的: ```java -/** - * @author : heibaiying - * @description : Memcached 操作基本对象 - */ @RunWith(SpringRunner.class) @ContextConfiguration({"classpath:springApplication.xml"}) public class MemSamples { @@ -158,13 +152,9 @@ public class MemSamples { ``` -#### 2.5 存储实体对象测试用例 +### 2.4 存储实体对象测试用例 ```java -/** - * @author : heibaiying - * @description :Memcached 序列化与反序列化 - */ @RunWith(SpringRunner.class) @ContextConfiguration({"classpath:springApplication.xml"}) public class MemObjectSamples { @@ -187,7 +177,7 @@ public class MemObjectSamples { -## 附:memcached 基本命令 +## 附:Memcached 基本命令 | 命令 | 格式 | 说明 | | --------------- | -------------------------------------------------- | ------------------------------------- | diff --git a/spring/spring-mongodb-annotation/README.md b/spring/spring-mongodb-annotation/README.md index da3d475..383f518 100644 --- a/spring/spring-mongodb-annotation/README.md +++ b/spring/spring-mongodb-annotation/README.md @@ -1,29 +1,29 @@ -# spring 整合 mongodb(注解方式) +# Spring 整合 MongoDB(注解方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring mongodb
-        2.1 新建配置文件及其映射类
+ + + +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 - -配置文件位于 com.heibaiying.config 下,项目以单元测试的方式进行测试。 +配置文件位于 com.heibaiying.config 包下,项目以单元测试的方式进行测试。
+### 1.2 相关依赖 -### 1.2 依赖说明 - -除了 spring 的基本依赖外,需要导入 mongodb 整合依赖包 +除了 Spring 的基本依赖外,需要导入 MongoDB 的整合依赖: ```xml @@ -36,9 +36,11 @@ -## 二、spring mongodb +## 二、整合 MongoDB -#### 2.1 新建配置文件及其映射类 +### 2.1 基本配置 + +新建配置文件及其映射类: ```properties mongo.host=192.168.200.228 @@ -60,10 +62,6 @@ mongo.socketTimeout=1500 ``` ```java -/** - * @author : heibaiying - * @description : Mongo 配置属性 - */ @Data @Configuration @PropertySource(value = "classpath:mongodb.properties") @@ -90,14 +88,9 @@ public class MongoProperty { } ``` -#### 2.2 整合配置 +### 2.2 整合配置 ```java -/** - * @author : heibaiying - * @description : Mongo 配置类 - */ - @Configuration @ComponentScan(value = "com.heibaiying.*") public class MongoConfig { @@ -122,7 +115,7 @@ public class MongoConfig { } ``` -#### 2.3 测试整合 +### 2.3 测试整合 ```java @RunWith(SpringRunner.class) diff --git a/spring/spring-mongodb/README.md b/spring/spring-mongodb/README.md index e9d1dbc..8226f6b 100644 --- a/spring/spring-mongodb/README.md +++ b/spring/spring-mongodb/README.md @@ -1,29 +1,27 @@ -# spring 整合 mongodb(xml配置方式) +# Spring 整合 MongoDB( XML 配置方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring mongodb
-        2.1 新建配置文件
+ +## 一、项目说明 -## 一、说明 - -### 1.1 项目结构说明 +### 1.1 项目结构 配置文件位于 resources 下,项目以单元测试的方式进行测试。
+### 1.2 相关依赖 -### 1.2 依赖说明 - -除了 spring 的基本依赖外,需要导入 mongodb 整合依赖包 +除了 Spring 的基本依赖外,需要导入 MongoDB 的整合依赖: ```xml @@ -36,9 +34,9 @@ -## 二、spring mongodb +## 二、整合 MongoDB -#### 2.1 新建配置文件 +### 2.1 配置文件 ```properties mongo.host=192.168.200.228 @@ -59,7 +57,7 @@ mongo.socketKeepAlive=true mongo.socketTimeout=1500 ``` -#### 2.2 整合配置 +### 2.2 整合配置 ```xml @@ -100,13 +98,9 @@ mongo.socketTimeout=1500 ``` -#### 2.3 测试整合 +### 2.3 测试整合 ```java -/** - * @author : heibaiying - * @description : MongoDB 查询 - */ @RunWith(SpringRunner.class) @ContextConfiguration(locations = "classpath:mongodb.xml") public class MongoDBTest { diff --git a/spring/spring-mybatis-annotation/README.md b/spring/spring-mybatis-annotation/README.md index a9028c9..05ed98a 100644 --- a/spring/spring-mybatis-annotation/README.md +++ b/spring/spring-mybatis-annotation/README.md @@ -1,27 +1,25 @@ -# spring 整合 mybatis(注解方式) +# Spring 整合 Mybatis(注解方式) -## 目录
-一、说明
-        1.1 项目结构
-        1.2 项目依赖
-二、spring 整合 mybatis
-        2.1 在resources文件夹下新建数据库配置文件jdbc.properties及其映射类
-        2.2 配置数据源和mybatis会话工厂、定义事务管理器
-        2.3 新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
-        2.4 新建查询接口及其对应的mapper文件
-        2.5 新建测试类进行测试
-## 正文
+ - -## 一、说明 +## 一、项目说明 #### 1.1 项目结构
- #### 1.2 项目依赖 -除了 spring 相关依赖外,还需要导入数据库驱动和对应的 mybatis 依赖包 +除了 Spring 相关依赖外,还需要导入数据库驱动和对应的 Mybatis 依赖: ```xml @@ -53,9 +51,11 @@
``` -## 二、spring 整合 mybatis +## 二、整合 Mybatis -#### 2.1 在resources文件夹下新建数据库配置文件jdbc.properties及其映射类 +#### 2.1 数据库配置 + +在 resources 文件夹下新建数据库配置文件 jdbc.properties: ```properties # mysql 数据库配置 @@ -89,12 +89,11 @@ public class DataSourceConfig { } ``` -#### 2.2 配置数据源和mybatis会话工厂、定义事务管理器 +#### 2.2 配置数据源 + +配置数据源、Mybatis 会话工厂和事务管理器: ```java -/** - * @author : heibaiying - */ @Configuration @EnableTransactionManagement // 开启声明式事务处理 等价于 xml 中 @ComponentScan(basePackages = {"com.heibaiying.*"}) @@ -123,7 +122,7 @@ public class DatabaseConfig { * 配置 mybatis 会话工厂 * * @param dataSource 这个参数的名称需要保持和上面方法名一致 才能自动注入,因为 - * 采用@Bean 注解生成的 bean 默认采用方法名为名称,当然也可以在使用@Bean 时指定 name 属性 + * 采用 @Bean 注解生成的 bean 默认采用方法名为名称,当然也可以通过name属性自行指定 */ @Bean public SqlSessionFactoryBean sessionFactoryBean(DriverManagerDataSource dataSource) throws IOException { @@ -155,12 +154,12 @@ public class DatabaseConfig { manager.setDataSource(dataSource); return manager; } - } - ``` -#### 2.3 新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) +#### 2.3 MyBatis 配置 + +新建mybtais配置文件,按照需求配置额外参数, 更多 settings 配置项可以参考 [官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) ```xml @@ -180,10 +179,11 @@ public class DatabaseConfig { - ``` -#### 2.4 新建查询接口及其对应的mapper文件 +#### 2.4 数据查询 + +新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表: ```java public interface MysqlDao { @@ -230,7 +230,9 @@ public interface OracleDao { ``` -#### 2.5 新建测试类进行测试 +#### 2.5 测试查询 + +新建测试类进行测试: ```java @RunWith(SpringRunner.class) @@ -270,6 +272,5 @@ public class OracleDaoTest { } } } - ``` diff --git a/spring/spring-mybatis/README.md b/spring/spring-mybatis/README.md index f727c26..c62e2ce 100644 --- a/spring/spring-mybatis/README.md +++ b/spring/spring-mybatis/README.md @@ -1,27 +1,25 @@ -# spring 整合 mybatis(xml配置方式) +# Spring 整合 Mybatis(XML 配置方式) -## 目录
-一、说明
-        1.1 项目结构
-        1.2 项目依赖
-二、spring 整合 mybatis
-        2.1 在resources文件夹下新建数据库配置文件jdbc.properties
-        2.2 配置数据源和mybatis会话工厂、定义事务管理器
-        2.3 新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html)
-        2.4 新建查询接口及其对应的mapper文件
-        2.5 新建测试类进行测试
-## 正文
+ - -## 一、说明 +## 一、项目说明 #### 1.1 项目结构
- #### 1.2 项目依赖 -除了 spring 相关依赖外,还需要导入数据库驱动和对应的 mybatis 依赖包 +除了 Spring 相关依赖外,还需要导入数据库驱动和对应的 Mybatis 依赖: ```xml @@ -53,9 +51,11 @@ ``` -## 二、spring 整合 mybatis +## 二、整合 Mybatis -#### 2.1 在resources文件夹下新建数据库配置文件jdbc.properties +#### 2.1 数据库配置 + +在 resources 文件夹下新建数据库配置文件 jdbc.properties: ```properties # mysql 数据库配置 @@ -71,7 +71,9 @@ oracle.username=用户名 oracle.password=密码 ``` -#### 2.2 配置数据源和mybatis会话工厂、定义事务管理器 +#### 2.2 配置数据源 + +配置数据源、Mybatis 会话工厂和事务管理器: ```xml @@ -79,7 +81,7 @@ oracle.password=密码 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> @@ -110,7 +112,7 @@ oracle.password=密码 - + @@ -131,7 +133,9 @@ oracle.password=密码 ``` -#### 2.3 新建mybtais配置文件,按照需求配置额外参数, 更多settings配置项可以参考[官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) +#### 2.3 MyBatis 配置 + +新建mybtais配置文件,按照需求配置额外参数, 更多 settings 配置项可以参考 [官方文档](http://www.mybatis.org/mybatis-3/zh/configuration.html) ```xml @@ -154,7 +158,9 @@ oracle.password=密码 ``` -#### 2.4 新建查询接口及其对应的mapper文件 +#### 2.4 数据查询 + +新建查询接口及其实现类,以下示例分别查询的是 MySQL 和 Oracle 中的字典表: ```java public interface MysqlDao { @@ -184,7 +190,6 @@ public interface OracleDao { List queryById(long id); } - ``` ```xml @@ -201,7 +206,9 @@ public interface OracleDao { ``` -#### 2.5 新建测试类进行测试 +#### 2.5 测试查询 + +新建测试类进行测试: ```java @RunWith(SpringRunner.class) @@ -221,7 +228,6 @@ public class MysqlDaoTest { } } } - ``` ```java @@ -244,6 +250,5 @@ public class OracleDaoTest { } } } - ``` diff --git a/spring/spring-rabbitmq-annotation/README.md b/spring/spring-rabbitmq-annotation/README.md index ef01d4c..0688431 100644 --- a/spring/spring-rabbitmq-annotation/README.md +++ b/spring/spring-rabbitmq-annotation/README.md @@ -1,43 +1,37 @@ -# spring 整合 rabbitmq(注解方式) +# Spring 整合 RabbitMQ(注解方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring rabbit 基本配置
-        2.1 基本配置属性及其映射类
-        2.2 创建连接工厂、管理器
-三、简单消费的发送
-        3.1 声明交换机、队列、绑定关系和消费者监听器
-        3.2 测试简单消息的发送
-四、传输对象
-        4.1 创建消息的委托处理器
-        4.2 声明交换机、队列、绑定关系和消费者监听器
-        4.3 测试对象消息的发送
-## 正文
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 - -1. 本用例关于 rabbitmq 的整合提供**简单消息发送**和**对象消费发送**两种情况下的 sample。 - -2. rabbitBaseAnnotation.java 中声明了 topic 类型的交换机、持久化队列、及其绑定关系,用于测试说明 topic 交换机路由键的绑定规则。 - -3. rabbitObjectAnnotation.java 中声明了 direct 类型的交换机,持久化队列,及其绑定关系,用于示例对象消息的传输。 - - 注:关于 rabbitmq 安装、交换机、队列、死信队列等基本概念可以参考我的手记[《RabbitMQ 实战指南》读书笔记](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ/%E3%80%8ARabbitMQ%E5%AE%9E%E6%88%98%E6%8C%87%E5%8D%97%E3%80%8B%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0.md),里面有详细的配图说明。 +1. 本用例关于 RabbitMQ 的整合提供**简单消息发送**和**对象消费发送**两种情况下的示例代码。 +2. `rabbitBaseAnnotation` 中声明了 topic 类型的交换机、持久化队列及其绑定关系,用于说明 topic 交换机的路由规则。 +3. `rabbitObjectAnnotation`中声明了 direct 类型的交换机,持久化队列及其绑定关系,用于示例对象消息的传输。
+### 1.2 基本依赖 -### 1.2 依赖说明 - -除了 spring 的基本依赖外,需要导入 spring rabbitmq 整合依赖 +除了 Spring 的基本依赖外,需要导入 Spring RabbitMQ 整合依赖: ```xml @@ -56,29 +50,26 @@ -## 二、spring rabbit 基本配置 +## 二、整合 RabbitMQ -#### 2.1 基本配置属性及其映射类 +### 2.1 基本配置 + +创建配置文件及其映射类: ```properties rabbitmq.addresses=localhost:5672 rabbitmq.username=guest rabbitmq.password=guest -# 虚拟主机,可以类比为命名空间 默认为/ 必须先用图形界面或者管控台添加 程序不会自动创建且会抛出异常 +# 虚拟主机,等价于名称空间,默认为 / ,如果想使用其他名称空间必须先用图形界面或者管控台添加,程序不会自动创建 rabbitmq.virtualhost=/ ``` ```java -/** - * @author : heibaiying - * @description : rabbit 属性配置 - */ @Data @PropertySource(value = "classpath:rabbitmq.properties") @Configuration public class RabbitProperty { - @Value("${rabbitmq.addresses}") private String addresses; @@ -93,10 +84,9 @@ public class RabbitProperty { } ``` -#### 2.2 创建连接工厂、管理器 +### 2.2 连接工厂与管理器 ```java - @Configuration @ComponentScan("com.heibaiying.rabbit.config") public class RabbitBaseConfig { @@ -136,21 +126,14 @@ public class RabbitBaseConfig { -## 三、简单消费的发送 +## 三、简单消息发送 -#### 3.1 声明交换机、队列、绑定关系和消费者监听器 +### 3.1 创建组件 + +声明交换机、队列、绑定关系和消费者监听器: ```java -import com.rabbitmq.client.Channel; -import org.springframework.amqp.core.*; -import org.springframework.amqp.rabbit.connection.ConnectionFactory; -import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; -import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - /** - * @author : heibaiying * @description : 声明队列、交换机、绑定关系、和队列消息监听 */ @@ -246,7 +229,7 @@ public class RabbitBaseAnnotation { } ``` -#### 3.2 测试简单消息的发送 +### 3.2 单元测试 ```java /** @@ -294,11 +277,11 @@ public class RabbitTest { -## 四、传输对象 +## 四、对象消息发送 -#### 4.1 创建消息的委托处理器 +### 4.1 委托处理器 -这里为了增强用例的实用性,我们创建的处理器的 handleMessage 方法是一个重载方法,对于同一个队列的监听,不仅可以传输对象消息,同时针对不同的对象类型调用不同的处理方法。 +这里为了增强用例的实用性,我们创建的一个委托处理器,并重载其 handleMessage 方法,从而可以针对不同类型的消息调用不同的处理方法: ```java /** @@ -318,7 +301,9 @@ public class MessageDelegate { } ``` -#### 4.2 声明交换机、队列、绑定关系和消费者监听器 +### 4.2 创建组件 + +声明交换机、队列、绑定关系和消费者监听器: ```java /** @@ -379,7 +364,7 @@ public class RabbitObjectAnnotation { } ``` -#### 4.3 测试对象消息的发送 +### 4.3 单元测试 ```java @RunWith(SpringRunner.class) diff --git a/spring/spring-rabbitmq/README.md b/spring/spring-rabbitmq/README.md index ef5e750..1a492f3 100644 --- a/spring/spring-rabbitmq/README.md +++ b/spring/spring-rabbitmq/README.md @@ -1,41 +1,36 @@ -# spring 整合 rabbitmq(xml配置方式) +# Spring 整合 RabbitMQ(XML 配置方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring rabbit 基本配置
-三、简单消费的发送
-        3.1 声明交换机、队列、绑定关系和消费者监听器
-        3.2 测试简单消息的发送
-四、传输对象
-        4.1 创建消息的委托处理器
-        4.2 声明交换机、队列、绑定关系和消费者监听器
-        4.3 测试对象消息的发送
-## 正文
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 +1. 本用例关于 RabbitMQ 的整合提供**简单消息发送**和**对象消费发送**两种情况下的示例代码。 -1. 本用例关于 rabbitmq 的整合提供**简单消息发送**和**对象消费发送**两种情况下的 sample。 +2. `rabbitBaseAnnotation` 中声明了 topic 类型的交换机、持久化队列及其绑定关系,用于说明 topic 交换机的路由规则。 -2. rabbitBaseAnnotation.java 中声明了 topic 类型的交换机、持久化队列、及其绑定关系,用于测试说明 topic 交换机路由键的绑定规则。 - -3. rabbitObjectAnnotation.java 中声明了 direct 类型的交换机,持久化队列,及其绑定关系,用于示例对象消息的传输。 - - 注:关于 rabbitmq 安装、交换机、队列、死信队列等基本概念可以参考我的手记[《RabbitMQ 实战指南》读书笔记](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ/%E3%80%8ARabbitMQ%E5%AE%9E%E6%88%98%E6%8C%87%E5%8D%97%E3%80%8B%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0.md),里面有详细的配图说明。 +3. `rabbitObjectAnnotation`中声明了 direct 类型的交换机,持久化队列及其绑定关系,用于示例对象消息的传输。
+### 1.2 基本依赖 - -### 1.2 依赖说明 - -除了 spring 的基本依赖外,需要导入 spring rabbitmq 整合依赖 +除了 Spring 的基本依赖外,需要导入 Spring RabbitMQ 整合依赖: ```xml @@ -54,13 +49,13 @@ -## 二、spring rabbit 基本配置 +## 二、整合 RabbitMQ ```properties rabbitmq.addresses=localhost:5672 rabbitmq.username=guest rabbitmq.password=guest -# 虚拟主机,可以类比为命名空间 默认为/ 必须先用图形界面或者管控台添加 程序不会自动创建且会抛出异常 +# 虚拟主机,等价于名称空间,默认为 / ,如果想使用其他名称空间必须先用图形界面或者管控台添加,程序不会自动创建 rabbitmq.virtualhost=/ ``` @@ -112,24 +107,13 @@ rabbitmq.virtualhost=/ -## 三、简单消费的发送 +## 三、简单消息发送 -#### 3.1 声明交换机、队列、绑定关系和消费者监听器 +### 3.1 创建组件 + +声明交换机、队列、绑定关系和消费者监听器: ```java -import com.rabbitmq.client.Channel; -import org.springframework.amqp.core.*; -import org.springframework.amqp.rabbit.connection.ConnectionFactory; -import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; -import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * @author : heibaiying - * @description : 声明队列、交换机、绑定关系、和队列消息监听 - */ - @Configuration public class RabbitBaseAnnotation { @@ -222,14 +206,9 @@ public class RabbitBaseAnnotation { } ``` -#### 3.2 测试简单消息的发送 +### 3.2 单元测试 ```java -/** - * @author : heibaiying - * @description : 传输简单字符串 - */ - @RunWith(SpringRunner.class) @ContextConfiguration(locations = "classpath:rabbitmq.xml") public class RabbitTest { @@ -262,23 +241,22 @@ public class RabbitTest { ```java 结果: - SecondQueue 收到消息:我的路由键 quick.orange.rabbit 符合 queue1 和 queue2 的要求,我应该被两个监听器接收到 - FirstQueue 收到消息:我的路由键 quick.orange.rabbit 符合 queue1 和 queue2 的要求,我应该被两个监听器接收到 - FirstQueue 收到消息:我的路由键 quick.orange.fox 只符合 queue1 的要求,只能被 queue 1 接收到 - SecondQueue 收到消息:我的路由键 lazy.brown.fox 只符合 queue2 的要求,只能被 queue 2 接收到 +SecondQueue 收到消息:我的路由键 quick.orange.rabbit 符合 queue1 和 queue2 的要求,我应该被两个监听器接收到 +FirstQueue 收到消息:我的路由键 quick.orange.rabbit 符合 queue1 和 queue2 的要求,我应该被两个监听器接收到 +FirstQueue 收到消息:我的路由键 quick.orange.fox 只符合 queue1 的要求,只能被 queue 1 接收到 +SecondQueue 收到消息:我的路由键 lazy.brown.fox 只符合 queue2 的要求,只能被 queue 2 接收到 ``` -## 四、传输对象 +## 四、对象消息发送 -#### 4.1 创建消息的委托处理器 +### 4.1 委托处理器 -这里为了增强用例的实用性,我们创建的处理器的 handleMessage 方法是一个重载方法,对于同一个队列的监听,不仅可以传输对象消息,同时针对不同的对象类型调用不同的处理方法。 +这里为了增强用例的实用性,我们创建的一个委托处理器,并重载其 handleMessage 方法,从而可以针对不同类型的消息调用不同的处理方法: ```java /** - * @author : heibaiying * @description :消息委派处理类 */ public class MessageDelegate { @@ -294,14 +272,11 @@ public class MessageDelegate { } ``` -#### 4.2 声明交换机、队列、绑定关系和消费者监听器 +### 4.2 创建组件 + +声明交换机、队列、绑定关系和消费者监听器: ```java -/** - * @author : heibaiying - * @description : 声明队列、交换机、绑定关系、用于测试对象的消息传递 - */ - @Configuration public class RabbitObjectAnnotation { @@ -355,7 +330,7 @@ public class RabbitObjectAnnotation { } ``` -#### 4.3 测试对象消息的发送 +### 4.3 单元测试 ```java @RunWith(SpringRunner.class) diff --git a/spring/spring-redis-annotation/README.md b/spring/spring-redis-annotation/README.md index a3b0f70..5d79b2e 100644 --- a/spring/spring-redis-annotation/README.md +++ b/spring/spring-redis-annotation/README.md @@ -1,77 +1,52 @@ -# spring 整合 redis (注解方式) - -## 目录
+# Spring 整合 Redis (注解方式) + ## 一、说明 -### 1.1 Redis 客户端说明 +### 1.1 客户端说明 -关于 spring 整合 mybatis 本用例提供两种整合方法: +关于 spring 整合 Redis 本用例提供两种整合方法: -1. jedis: 官方推荐的 java 客户端,能够胜任 redis 的大多数基本使用; -2. redisson:也是官方推荐的客户端,比起 jedis 提供了更多高级的功能,比如分布式锁、集合数据切片等功能。同时提供了丰富而全面的中英文版本的 wiki。 +- **Jedis**: 官方推荐的 java 客户端,能够胜任 Redis 的大多数基本使用; -注:关于 redis 其他语言官方推荐的客户端可以在[客户端](http://www.redis.cn/clients.html) 该网页查看,其中官方推荐的用了黄色星星:star:标注。 +- **Redisson**:也是官方推荐的客户端,比起 Redisson 提供了更多高级的功能,如分布式锁、集合数据切片等功能。同时提供了丰富而全面的中英文版本的说明文档。 + + Redis 所有语言官方推荐的客户端可以在 [客户端](http://www.redis.cn/clients.html) 该网页查看,其中官方推荐的客户端使用了:star:进行标注:
+### 1.2 可视化软件 - -### 1.2 Redis可视化软件 - -推荐**Redis Desktop Manager** 作为可视化查看工具,可以直观看到用例中测试关于存储实体对象序列化的情况。 +推荐使用 **Redis Desktop Manager** 作为可视化查看工具,可以直观看到存储的数据及其序列化的情况。 ### 1.3 项目结构说明 -1. jedis 和 redisson 的配置类和单元测试分别位于 config 和 test 下对应的包中,其中集群的配置类以 cluster 开头。 -2. 实体类 Programmer.java 用于测试 Redisson 序列化与反序列化 ++ Jedis 和 Redisson 的配置类和单元测试分别位于 config 和 test 下对应的包中,其中集群的配置类以 cluster 开头。 + ++ 实体类 Programmer.java 用于测试 Redisson 序列化与反序列化。
+### 1.4 依赖说明 - -### 1.3 依赖说明 - -除了 spring 的基本依赖外,需要导入 jedis 和 redisson 对应的客户端依赖包 +除了 Spring 的基本依赖外,需要导入 Jedis 和 Redisson 对应的客户端依赖: ```xml @@ -96,7 +71,9 @@ ## 二、spring 整合 jedis -#### 2.1 新建基本配置文件和其映射类 +### 2.1 新建配置文件 + +新建配置文件及其映射类: ```properties redis.host=127.0.0.1 @@ -130,15 +107,9 @@ public class RedisProperty { } ``` - - -#### 2.2 单机配置 +### 2.2 单机配置 ```java -/** - * @author : heibaiying - * @description : Jedis 单机配置 - */ @Configuration @ComponentScan(value = "com.heibaiying.*") public class SingleJedisConfig { @@ -158,7 +129,7 @@ public class SingleJedisConfig { } ``` -#### 2.3 集群配置 +### 2.3 集群配置 ```java @Configuration @@ -178,17 +149,13 @@ public class ClusterJedisConfig { } ``` -#### 2.4 单机版本测试用例 +### 2.4 单机版本测试用例 -1.需要注意的是,对于 jedis 而言,单机版本和集群版本注入的实例是不同的; +- 需要注意的是,对于 Jedis 而言,单机版本和集群版本注入的实例是不同的; -2.jedis 本身并不支持序列化于反序列化操作,如果需要存储实体类,需要序列化后存入。(redisson 本身就支持序列化于反序列化,详见下文) +- Jedis 本身并不支持序列化于反序列化操作,如果需要存储实体类,需要序列化后存入,而 Redisson 本身就支持序列化于反序列化操作,详见下文)。 ```java -/** - * @author : heibaiying - * @description :redis 单机版测试 - */ @RunWith(SpringRunner.class) @ContextConfiguration(classes = SingleJedisConfig.class) public class JedisSamples { @@ -216,7 +183,7 @@ public class JedisSamples { } ``` -#### 2.5 集群版本测试用例 +### 2.5 集群版本测试用例 ```java @RunWith(SpringRunner.class) @@ -242,22 +209,16 @@ public class JedisClusterSamples { String s = jedisCluster.setex("spring", 10, "我会在 10 秒后过期"); System.out.println(s); } - - } ``` -## 三、spring 整合 redisson +## 三、Spring 整合 Redisson -#### 2.1 单机配置 +### 2.1 单机配置 ```java -/** - * @author : heibaiying - * @description : redisson 单机配置 - */ @Configuration public class SingalRedissonConfig { @@ -268,17 +229,12 @@ public class SingalRedissonConfig { config.useSingleServer().setAddress("redis://127.0.0.1:6379"); return Redisson.create(config); } - } ``` -#### 2.2 集群配置 +### 2.2 集群配置 ```java -/** - * @author : heibaiying - * @description : redisson 集群配置 - */ @Configuration public class ClusterRedissonConfig { @@ -292,20 +248,14 @@ public class ClusterRedissonConfig { .addNodeAddress("redis://127.0.0.1:6381"); return Redisson.create(config); } - } ``` -#### 2.3 存储基本类型测试用例 +### 2.3 存储基本类型测试用例 -1. 这里需要注意的是,对于 Redisson 而言, 单机和集群最后在使用的时候注入的都是 RedissonClient,这和 jedis 是不同的。 +需要注意的是,对于 Redisson 而言, 单机和集群在使用的时候注入的都是 RedissonClient,这和 Jedis 是不同的。 ```java -/** - * @author : heibaiying - * @description :redisson 操作普通数据类型 - */ - @RunWith(SpringRunner.class) @ContextConfiguration(classes = SingalRedissonConfig.class) public class RedissonSamples { @@ -341,18 +291,11 @@ public class RedissonSamples { redissonClient.shutdown(); } } - ``` -#### 2.4 存储实体对象测试用例 +### 2.4 存储实体对象测试用例 ```java -/** - * @author : heibaiying - * @description :redisson 对象序列化与反序列化 - */ - - @RunWith(SpringRunner.class) @ContextConfiguration(classes = SingalRedissonConfig.class) public class RedissonObjectSamples { @@ -360,14 +303,13 @@ public class RedissonObjectSamples { @Autowired private RedissonClient redissonClient; - // Redisson 的对象编码类是用于将对象进行序列化和反序列化 默认采用 Jackson - @Test public void Set() { RBucket rBucket = redissonClient.getBucket("programmer"); rBucket.set(new Programmer("xiaoming", 12, 5000.21f, new Date())); redissonClient.shutdown(); - //存储结果: {"@class":"com.heibaiying.com.heibaiying.bean.Programmer","age":12,"birthday":["java.util.Date",1545714986590],"name":"xiaoming","salary":5000.21} + // Redisson 的对象编码类是用于将对象进行序列化和反序列化 默认采用 Jackson + // 存储结果: {"@class":"com.heibaiying.com.heibaiying.bean.Programmer","age":12,"birthday":["java.util.Date",1545714986590],"name":"xiaoming","salary":5000.21} } @Test @@ -382,190 +324,3 @@ public class RedissonObjectSamples { } } ``` - -## 附:Redis的数据结构和操作命令 - -### 1.1 预备 - -#### 1.1.1 全局命令 - -1. 查看所有键: **keys \*** - -2. 查看键总数:**dbsize** - -3. 检查键是否存在:**exists key** - -4. 删除键:**del key [key ...]** 支持删除多个键 - -5. 键过期:**expire key seconds** - - ttl 命令会返回键的剩余过期时间, 它有 3 种返回值: - - - 大于等于 0 的整数: 键剩余的过期时间。 - - -1: 键没设置过期时间。 - - -2: 键不存在 - -6. 键的数据结构 **type key** - -#### 1.1.2 数据结构和内部编码 - -type 命令实际返回的就是当前键的数据结构类型, 它们分别是:**string**(字符串) 、 **hash**(哈希) 、 **list**(列表) 、 **set**(集合) 、 **zset**(有序集合) - -#### 1.1.3 单线程架构 - -1. 纯内存访问, Redis 将所有数据放在内存中, 内存的响应时长大约为 100 纳秒, 这是 Redis 达到每秒万级别访问的重要基础。 -2. 非阻塞 I/O, Redis 使用 epoll 作为 I/O 多路复用技术的实现, 再加上 Redis 自身的事件处理模型将 epoll 中的连接、 读写、 关闭都转换为事件, 不在网络 I/O 上浪费过多的时间, 如图 2-6 所示。 -3. 单线程避免了线程切换和竞态产生的消耗。 - -### 1.2 字符串 - -| 作用 | 格式 | 参数或示例 | -| ---------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 设置值 | set key value \[ex seconds]\[px milliseconds][nx\|xx] setnx setex | ex seconds: 为键设置秒级过期时间。
px milliseconds: 为键设置毫秒级过期时间。
nx: 键必须不存在, 才可以设置成功, 用于添加。
xx: 与 nx 相反, 键必须存在, 才可以设置成功, 用于更新。 | -| 获取值 | get key | r 如果获取的键不存在 ,则返回 nil(空) | -| 批量设置 | mset key value [key value ...] | mset a 1 b 2 c 3 d 4 | -| 批量获取值 | mget key [key ...] | mget a b c d | -| 计数 | incr key decr key incrby key increment(指定数值自增)
decrby key decrement(指定数值自减)
incrbyfloat key increment (浮点数自增) | 值不是整数, 返回错误。 值是整数, 返回自增或自减后的结果。
键不存在,创建键,并按照值为 0 自增或自减, 返回结果为 1。 | -| 追加值 | append key value | 向字符串的默认追加值 | -| 字符串长度 | strlen key | 获取字符串长度,中文占用三个字节 | -| 设置并返回原值 | getset key value | | -| 设置指定位置的租字符串 | setrange key offeset value | | -| 获取部分字符串 | getrange key start end | | - -### 1.3 哈希 - -| 作用 | 格式 | 参数或示例 | -| ------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 设置值 | hset key field value | hset user:1 name tom
hset user:1 age 12 | -| 获取值 | hget key field | hget user:1 name | -| 删除 field | hdel key field [field ...] | | -| 计算 field 个数 | hlen key | | -| 批量设置或获取 field-value | hmget key field [field]
hmset key field value [field value...] | hmset user:1 name mike age 12 city tianjin
hmget user:1 name city | -| 判断 field 是否存在 | hexists key field | | -| 获取所有 field | hkeys key | | -| 获取所有 value | hvals key | | -| 获取所有的 filed-value | hgetall key | 如果哈希元素个数比较多, 会存在阻塞 Redis 的可能。
获取全部 可以使用 hscan 命令, 该命令会渐进式遍历哈希类型 | -| 计数 | hincrby key field
hincrbyfloat key field | | - -### 1.4 列表 - -| 作用 | 格式 | 参数或示例 | -| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 增 | 左侧插入:lpush key value [value ...] 右侧插入:rpush key value [value ...] 某个指定元素前后插入:linsert key before\|after pivot value | | -| 查 | 获取指定范围内的元素列表:lrange key start end 获取列表指定索引下标的元素:lindex key index 获取列表指定长度:llen key | lrange listkey 0 -1 | -| 删 | 从列表左侧弹出元素:lpop key 从列表右侧弹出元素:rpop key 删除指定元素:lrem key count value 截取列表:ltrim key start end | count>0, 从左到右, 删除最多 count 个元素。
count<0, 从右到左, 删除最多 count 绝对值个元素。
count=0, 删除所有 | -| 改 | 修改指定索引下标的元素:lset key index newValue | | -| 阻塞操作 | blpop key [key ...] timeout brpop key [key ...] timeout | key[key...]: 多个列表的键。 timeout: 阻塞时间\|等待时间(单位: 秒) | - - - -### 1.5 集合 - -集合(set) 类型也是用来保存多个的字符串元素, 但和列表类型不一样的是, **集合中不允许有重复元素**, 并且集合中的元素是无序的, **不能通过索引下标获取元素**。 - -**集合内操作**: - -| 作用 | 格式 | 参数或示例 | -| -------------------- | ------------------------------ | ----------------------------------------- | -| 添加元素 | sadd key element [element ...] | 返回结果为添加成功的元素个数 | -| 删除元素 | srem key element [element ...] | 返回结果为成功删除的元素个数 | -| 计算元素个数 | scard key | | -| 判断元素是否在集合中 | sismember key element | | -| 随机返回 | srandmember key [count] | 随机从集合返回指定个数元素,count 默认为 1 | -| 从集合随机弹出元素 | spop key | srandmember 不会从集合中删除元素,spop 会 | -| 获取集合中所有元素 | smembers key | 可用 sscan 代替 | - -**集合间操作**: - -| 作用 | 格式 | -| ---------------------------- | ------------------------------------------------------------ | -| 求多个集合的交集 | sinter key [key ...] | -| 求多个集合的并集 | suinon key [key ...] | -| 求多个集合的差集 | sdiff key [key ...] | -| 将交集、并集、差集的结果保存 | sinterstore destination key [key ...]
suionstore destination key [key ...]
sdiffstore destination key [key ...] | - -### 1.6 有序集合 - -有序集合中的元素可以排序。 但是它和列表使用索引下标作为排序依据不同的是, 它给每个元素设置一个分数(score) 作为排序的依据。 - -**集合内操作**: - -| 作用 | 格式 | 参数或示例 | -| ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 添加成员 | zadd key score member [score member ...] | nx: member 必须不存在, 才可设置成功, 用于添加。
xx: member 必须存在, 才可以设置成功, 用于更新。
ch: 返回此次操作后, 有序集合元素和分数发生变化的个数
incr: 对 score 做增加, 相当于后面介绍的 zincrby。 | -| 计算成员个数 | zcard key | | -| 计算某个成员的分数 | zscore key member | | -| 计算某个成员的排名 | zrank key member zrevrank key member | zrank 是从分数从低到高返回排名, zrevrank 反之。 | -| 删除成员 | zrem key member [member ...] | | -| 增加成员分数 | zincrby key increment member | zincrby user:ranking 9 tom | -| 返回指定排名范围的成员 | zrange key start end [withscores] zrange key start end [withscores] | zrange 是从低到高返回, zrevrange 反之。 | -| 返回指定分数范围内的成员 | zrangebyscore key min max \[withscores][limit offset count] zrevrangebyscore key max min \[withscores][limit offset count] | 其中 zrangebyscore 按照分数从低到高返回, zrevrangebyscore 反之。 [limit offset count]选项可以限制输出的起始位置和个数: 同时 min 和 max 还支持开区间(小括号) 和闭区间(中括号) , -inf 和 +inf 分别代表无限小和无限大 | -| 删除指定排名内的升序元素 | zremrangerank key start end | | -| 删除指定分数范围的成员 | zremrangebyscore key min max | | - -**集合间操作**: - -| 作用 | 格式 | -| ---- | ------------------------------------------------------------ | -| 交集 | zinterstore destination numkeys key \[key ...] [weights weight [weight ...]] \[aggregate sum\|min\|max] | -| 并集 | zunionstore destination numkeys key \[key ...] [weights weight [weight ...]] \[aggregate sum\|min\|max] | - -- destination: 交集计算结果保存到这个键。 -- numkeys: 需要做交集计算键的个数。 -- key[key...]: 需要做交集计算的键。 -- weights weight[weight...]: 每个键的权重, 在做交集计算时, 每个键中的每个 member 会将自己分数乘以这个权重, 每个键的权重默认是 1。 -- aggregate sum|min|max: 计算成员交集后, 分值可以按照 sum(和) 、min(最小值) 、 max(最大值) 做汇总, 默认值是 sum。 - -### 1.7 键管理 - -#### 1.7.1 单个键管理 - -##### 1.键重命名 - -**rename key newkey** - - 为了防止被强行 rename, Redis 提供了 renamenx 命令, 确保只有 newKey 不存在时候才被覆盖。 - -##### 2. 随机返回键 - - **random key** - -##### 3.键过期 - -- expire key seconds: 键在 seconds 秒后过期。 -- expireat key timestamp: 键在秒级时间戳 timestamp 后过期。 -- pexpire key milliseconds: 键在 milliseconds 毫秒后过期。 -- pexpireat key milliseconds-timestamp 键在毫秒级时间戳 timestamp 后过期 - -注意: - -1. 如果 expire key 的键不存在, 返回结果为 0 -2. 如果设置过期时间为负值, 键会立即被删除, 犹如使用 del 命令一样 -3. persist key t 命令可以将键的过期时间清除 -4. 对于字符串类型键, 执行 set 命令会去掉过期时间, 这个问题很容易在开发中被忽视 -5. Redis 不支持二级数据结构(例如哈希、 列表) 内部元素的过期功能, 例如不能对列表类型的一个元素做过期时间设置 -6. setex 命令作为 set+expire 的组合, 不但是原子执行, 同时减少了一次网络通讯的时间 - -#### 1.7.2 键遍历 - -##### 1. 全量键遍历 - -**keys pattern** - -##### 2. 渐进式遍历 - -scan cursor \[match pattern] \[count number] - -- cursor 是必需参数, 实际上 cursor 是一个游标, 第一次遍历从 0 开始, 每次 scan 遍历完都会返回当前游标的值, 直到游标值为 0, 表示遍历结束。 -- match pattern 是可选参数, 它的作用的是做模式的匹配, 这点和 keys 的模式匹配很像。 -- count number 是可选参数, 它的作用是表明每次要遍历的键个数, 默认值是 10, 此参数可以适当增大。 - -#### 1.7.3 数据库管理 - -##### 1.切换数据库 - -**select dbIndex** - -##### 2.flushdb/flushall - -flushdb/flushall 命令用于清除数据库, 两者的区别的是 flushdb 只清除当前数据库, flushall 会清除所有数据库。 diff --git a/spring/spring-redis/README.md b/spring/spring-redis/README.md index 4d98066..b6dea36 100644 --- a/spring/spring-redis/README.md +++ b/spring/spring-redis/README.md @@ -1,72 +1,50 @@ -# spring 整合 redis (xml配置方式) +# Spring 整合 Redis ( XML配置方式) -## 目录
+ ## 一、说明 -### 1.1 Redis 客户端说明 +### 1.1 客户端说明 -关于 spring 整合 mybatis 本用例提供两种整合方法: +关于 spring 整合 Redis 本用例提供两种整合方法: -1. jedis: 官方推荐的 java 客户端,能够胜任 redis 的大多数基本使用; -2. redisson:也是官方推荐的客户端,比起 jedis 提供了更多高级的功能,比如分布式锁、集合数据切片等功能。同时提供了丰富而全面的中英文版本的 wiki。 ++ **Jedis**: 官方推荐的 java 客户端,能够胜任 Redis 的大多数基本使用; -注:关于 redis 其他语言官方推荐的客户端可以在[客户端](http://www.redis.cn/clients.html) 该网页查看,其中官方推荐的用了黄色星星:star:标注。 ++ **Redisson**:也是官方推荐的客户端,比起 Redisson 提供了更多高级的功能,如分布式锁、集合数据切片等功能。同时提供了丰富而全面的中英文版本的说明文档。 + + Redis 所有语言官方推荐的客户端可以在 [客户端](http://www.redis.cn/clients.html) 该网页查看,其中官方推荐的客户端使用了:star:进行标注:
+### 1.2 可视化软件 -### 1.2 Redis可视化软件 +推荐使用 **Redis Desktop Manager** 作为可视化查看工具,可以直观看到存储的数据及其序列化的情况。 -推荐**Redis Desktop Manager** 作为可视化查看工具,可以直观看到用例中测试关于存储实体对象序列化的情况。 +### 1.3 项目结构 -### 1.3 项目结构说明 ++ Jedis 和 Redisson 的配置和单元测试分别位于 resources 和 test 下对应的包中,其中集群的配置文件以 cluster 结尾。所有配置按需在 `springApplication.xml` 用 import 标签导入。 -1. jedis 和 redisson 的配置和单元测试分别位于 resources 和 test 下对应的包中,其中集群的配置文件以 cluster 结尾。所有配置按照需要在 springApplication.xml 用 import 导入。 -2. 实体类 Programmer.java 用于测试 Redisson 序列化与反序列化 ++ 实体类 Programmer 用于测试 Redisson 序列化与反序列化。
- **springapplication.xml 文件:** ```xml @@ -90,9 +68,9 @@ ``` -### 1.3 依赖说明 +### 1.4 依赖说明 -除了 spring 的基本依赖外,需要导入 jedis 和 redisson 对应的客户端依赖包 +除了 Spring 的基本依赖外,需要导入 Jedis 和 Redisson 对应的客户端依赖: ```xml @@ -115,9 +93,9 @@ -## 二、spring 整合 jedis +## 二、Spring 整合 Jedis -#### 2.1 新建基本配置文件 +### 2.1 新建配置文件 ```properties redis.host=127.0.0.1 @@ -130,7 +108,7 @@ redis.maxIdle=8 redis.maxTotal=16 ``` -#### 2.2 单机配置 +### 2.2 单机配置 ```xml @@ -163,7 +141,7 @@ redis.maxTotal=16 ``` -#### 2.3 集群配置 +### 2.3 集群配置 ```xml @@ -206,11 +184,11 @@ redis.maxTotal=16 ``` -#### 2.4 单机版本测试用例 +### 2.4 单机版本测试用例 -1.需要注意的是,对于 jedis 而言,单机版本和集群版本注入的实例是不同的; ++ 需要注意的是,对于 Jedis 而言,单机版本和集群版本注入的实例是不同的; -2.jedis 本身并不支持序列化于反序列化操作,如果需要存储实体类,需要序列化后存入。(redisson 本身就支持序列化于反序列化,详见下文) ++ Jedis 本身并不支持序列化于反序列化操作,如果需要存储实体类,需要序列化后存入,而 Redisson 本身就支持序列化于反序列化操作,详见下文)。 ```java @RunWith(SpringRunner.class) @@ -241,7 +219,7 @@ public class JedisSamples { ``` -#### 2.5 集群版本测试用例 +### 2.5 集群版本测试用例 ```java @@ -274,9 +252,9 @@ public class JedisClusterSamples { -## 三、spring 整合 redisson +## 三、Spring 整合 Redisson -#### 2.1 单机配置 +### 2.1 单机配置 ```xml @@ -309,7 +287,7 @@ public class JedisClusterSamples { ``` -#### 2.2 集群配置 +### 2.2 集群配置 ```xml @@ -332,9 +310,9 @@ public class JedisClusterSamples { ``` -#### 2.3 存储基本类型测试用例 +### 2.3 存储基本类型测试用例 -1. 这里需要注意的是,对于 Redisson 而言, 单机和集群最后在使用的时候注入的都是 RedissonClient,这和 jedis 是不同的。 +需要注意的是,对于 Redisson 而言, 单机和集群在使用的时候注入的都是 RedissonClient,这和 Jedis 是不同的。 ```java @RunWith(SpringRunner.class) @@ -373,7 +351,7 @@ public class RedissonSamples { } ``` -#### 2.4 存储实体对象测试用例 +### 2.4 存储实体对象测试用例 ```java @RunWith(SpringRunner.class) @@ -383,13 +361,12 @@ public class RedissonObjectSamples { @Autowired private RedissonClient redissonClient; - // Redisson 的对象编码类是用于将对象进行序列化和反序列化 默认采用 Jackson - @Test public void Set() { RBucket rBucket = redissonClient.getBucket("programmer"); rBucket.set(new Programmer("xiaoming", 12, 5000.21f, new Date())); - //存储结果: {"@class":"com.heibaiying.bean.Programmer","age":12,"birthday":["java.util.Date",1545714986590],"name":"xiaoming","salary":5000.21} + // Redisson 默认采用 Jackson 将对象进行序列化和反序列化 + // 存储结果: {"@class":"com.heibaiying.bean.Programmer","age":12,"birthday":["java.util.Date",1545714986590],"name":"xiaoming","salary":5000.21} } @Test @@ -403,192 +380,5 @@ public class RedissonObjectSamples { redissonClient.shutdown(); } } - ``` -## 附:Redis的数据结构和操作命令 - -### 1.1 预备 - -#### 1.1.1 全局命令 - -1. 查看所有键: **keys \*** - -2. 查看键总数:**dbsize** - -3. 检查键是否存在:**exists key** - -4. 删除键:**del key [key ...]** 支持删除多个键 - -5. 键过期:**expire key seconds** - - ttl 命令会返回键的剩余过期时间, 它有 3 种返回值: - - - 大于等于 0 的整数: 键剩余的过期时间。 - - -1: 键没设置过期时间。 - - -2: 键不存在 - -6. 键的数据结构 **type key** - -#### 1.1.2 数据结构和内部编码 - -type 命令实际返回的就是当前键的数据结构类型, 它们分别是:**string**(字符串) 、 **hash**(哈希) 、 **list**(列表) 、 **set**(集合) 、 **zset**(有序集合) - -#### 1.1.3 单线程架构 - -1. 纯内存访问, Redis 将所有数据放在内存中, 内存的响应时长大约为 100 纳秒, 这是 Redis 达到每秒万级别访问的重要基础。 -2. 非阻塞 I/O, Redis 使用 epoll 作为 I/O 多路复用技术的实现, 再加上 Redis 自身的事件处理模型将 epoll 中的连接、 读写、 关闭都转换为事件, 不在网络 I/O 上浪费过多的时间, 如图 2-6 所示。 -3. 单线程避免了线程切换和竞态产生的消耗。 - -### 1.2 字符串 - -| 作用 | 格式 | 参数或示例 | -| ---------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 设置值 | set key value \[ex seconds]\[px milliseconds][nx\|xx] setnx setex | ex seconds: 为键设置秒级过期时间。
px milliseconds: 为键设置毫秒级过期时间。
nx: 键必须不存在, 才可以设置成功, 用于添加。
xx: 与 nx 相反, 键必须存在, 才可以设置成功, 用于更新。 | -| 获取值 | get key | r 如果获取的键不存在 ,则返回 nil(空) | -| 批量设置 | mset key value [key value ...] | mset a 1 b 2 c 3 d 4 | -| 批量获取值 | mget key [key ...] | mget a b c d | -| 计数 | incr key decr key incrby key increment(指定数值自增)
decrby key decrement(指定数值自减)
incrbyfloat key increment (浮点数自增) | 值不是整数, 返回错误。 值是整数, 返回自增或自减后的结果。
键不存在,创建键,并按照值为 0 自增或自减, 返回结果为 1。 | -| 追加值 | append key value | 向字符串的默认追加值 | -| 字符串长度 | strlen key | 获取字符串长度,中文占用三个字节 | -| 设置并返回原值 | getset key value | | -| 设置指定位置的租字符串 | setrange key offeset value | | -| 获取部分字符串 | getrange key start end | | - -### 1.3 哈希 - -| 作用 | 格式 | 参数或示例 | -| ------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 设置值 | hset key field value | hset user:1 name tom
hset user:1 age 12 | -| 获取值 | hget key field | hget user:1 name | -| 删除 field | hdel key field [field ...] | | -| 计算 field 个数 | hlen key | | -| 批量设置或获取 field-value | hmget key field [field]
hmset key field value [field value...] | hmset user:1 name mike age 12 city tianjin
hmget user:1 name city | -| 判断 field 是否存在 | hexists key field | | -| 获取所有 field | hkeys key | | -| 获取所有 value | hvals key | | -| 获取所有的 filed-value | hgetall key | 如果哈希元素个数比较多, 会存在阻塞 Redis 的可能。
获取全部 可以使用 hscan 命令, 该命令会渐进式遍历哈希类型 | -| 计数 | hincrby key field
hincrbyfloat key field | | - -### 1.4 列表 - -| 作用 | 格式 | 参数或示例 | -| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 增 | 左侧插入:lpush key value [value ...] 右侧插入:rpush key value [value ...] 某个指定元素前后插入:linsert key before\|after pivot value | | -| 查 | 获取指定范围内的元素列表:lrange key start end 获取列表指定索引下标的元素:lindex key index 获取列表指定长度:llen key | lrange listkey 0 -1 | -| 删 | 从列表左侧弹出元素:lpop key 从列表右侧弹出元素:rpop key 删除指定元素:lrem key count value 截取列表:ltrim key start end | count>0, 从左到右, 删除最多 count 个元素。
count<0, 从右到左, 删除最多 count 绝对值个元素。
count=0, 删除所有 | -| 改 | 修改指定索引下标的元素:lset key index newValue | | -| 阻塞操作 | blpop key [key ...] timeout brpop key [key ...] timeout | key[key...]: 多个列表的键。 timeout: 阻塞时间\|等待时间(单位: 秒) | - - - -### 1.5 集合 - -集合(set) 类型也是用来保存多个的字符串元素, 但和列表类型不一样的是, **集合中不允许有重复元素**, 并且集合中的元素是无序的, **不能通过索引下标获取元素**。 - -**集合内操作**: - -| 作用 | 格式 | 参数或示例 | -| -------------------- | ------------------------------ | ----------------------------------------- | -| 添加元素 | sadd key element [element ...] | 返回结果为添加成功的元素个数 | -| 删除元素 | srem key element [element ...] | 返回结果为成功删除的元素个数 | -| 计算元素个数 | scard key | | -| 判断元素是否在集合中 | sismember key element | | -| 随机返回 | srandmember key [count] | 随机从集合返回指定个数元素,count 默认为 1 | -| 从集合随机弹出元素 | spop key | srandmember 不会从集合中删除元素,spop 会 | -| 获取集合中所有元素 | smembers key | 可用 sscan 代替 | - -**集合间操作**: - -| 作用 | 格式 | -| ---------------------------- | ------------------------------------------------------------ | -| 求多个集合的交集 | sinter key [key ...] | -| 求多个集合的并集 | suinon key [key ...] | -| 求多个集合的差集 | sdiff key [key ...] | -| 将交集、并集、差集的结果保存 | sinterstore destination key [key ...]
suionstore destination key [key ...]
sdiffstore destination key [key ...] | - -### 1.6 有序集合 - -有序集合中的元素可以排序。 但是它和列表使用索引下标作为排序依据不同的是, 它给每个元素设置一个分数(score) 作为排序的依据。 - -**集合内操作**: - -| 作用 | 格式 | 参数或示例 | -| ------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | -| 添加成员 | zadd key score member [score member ...] | nx: member 必须不存在, 才可设置成功, 用于添加。
xx: member 必须存在, 才可以设置成功, 用于更新。
ch: 返回此次操作后, 有序集合元素和分数发生变化的个数
incr: 对 score 做增加, 相当于后面介绍的 zincrby。 | -| 计算成员个数 | zcard key | | -| 计算某个成员的分数 | zscore key member | | -| 计算某个成员的排名 | zrank key member zrevrank key member | zrank 是从分数从低到高返回排名, zrevrank 反之。 | -| 删除成员 | zrem key member [member ...] | | -| 增加成员分数 | zincrby key increment member | zincrby user:ranking 9 tom | -| 返回指定排名范围的成员 | zrange key start end [withscores] zrange key start end [withscores] | zrange 是从低到高返回, zrevrange 反之。 | -| 返回指定分数范围内的成员 | zrangebyscore key min max \[withscores][limit offset count] zrevrangebyscore key max min \[withscores][limit offset count] | 其中 zrangebyscore 按照分数从低到高返回, zrevrangebyscore 反之。 [limit offset count]选项可以限制输出的起始位置和个数: 同时 min 和 max 还支持开区间(小括号) 和闭区间(中括号) , -inf 和 +inf 分别代表无限小和无限大 | -| 删除指定排名内的升序元素 | zremrangerank key start end | | -| 删除指定分数范围的成员 | zremrangebyscore key min max | | - -**集合间操作**: - -| 作用 | 格式 | -| ---- | ------------------------------------------------------------ | -| 交集 | zinterstore destination numkeys key \[key ...] [weights weight [weight ...]] \[aggregate sum\|min\|max] | -| 并集 | zunionstore destination numkeys key \[key ...] [weights weight [weight ...]] \[aggregate sum\|min\|max] | - -- destination: 交集计算结果保存到这个键。 -- numkeys: 需要做交集计算键的个数。 -- key[key...]: 需要做交集计算的键。 -- weights weight[weight...]: 每个键的权重, 在做交集计算时, 每个键中的每个 member 会将自己分数乘以这个权重, 每个键的权重默认是 1。 -- aggregate sum|min|max: 计算成员交集后, 分值可以按照 sum(和) 、min(最小值) 、 max(最大值) 做汇总, 默认值是 sum。 - -### 1.7 键管理 - -#### 1.7.1 单个键管理 - -##### 1.键重命名 - -**rename key newkey** - - 为了防止被强行 rename, Redis 提供了 renamenx 命令, 确保只有 newKey 不存在时候才被覆盖。 - -##### 2. 随机返回键 - - **random key** - -##### 3.键过期 - -- expire key seconds: 键在 seconds 秒后过期。 -- expireat key timestamp: 键在秒级时间戳 timestamp 后过期。 -- pexpire key milliseconds: 键在 milliseconds 毫秒后过期。 -- pexpireat key milliseconds-timestamp 键在毫秒级时间戳 timestamp 后过期 - -注意: - -1. 如果 expire key 的键不存在, 返回结果为 0 -2. 如果设置过期时间为负值, 键会立即被删除, 犹如使用 del 命令一样 -3. persist key t 命令可以将键的过期时间清除 -4. 对于字符串类型键, 执行 set 命令会去掉过期时间, 这个问题很容易在开发中被忽视 -5. Redis 不支持二级数据结构(例如哈希、 列表) 内部元素的过期功能, 例如不能对列表类型的一个元素做过期时间设置 -6. setex 命令作为 set+expire 的组合, 不但是原子执行, 同时减少了一次网络通讯的时间 - -#### 1.7.2 键遍历 - -##### 1. 全量键遍历 - -**keys pattern** - -##### 2. 渐进式遍历 - -scan cursor \[match pattern] \[count number] - -- cursor 是必需参数, 实际上 cursor 是一个游标, 第一次遍历从 0 开始, 每次 scan 遍历完都会返回当前游标的值, 直到游标值为 0, 表示遍历结束。 -- match pattern 是可选参数, 它的作用的是做模式的匹配, 这点和 keys 的模式匹配很像。 -- count number 是可选参数, 它的作用是表明每次要遍历的键个数, 默认值是 10, 此参数可以适当增大。 - -#### 1.7.3 数据库管理 - -##### 1.切换数据库 - -**select dbIndex** - -##### 2.flushdb/flushall - -flushdb/flushall 命令用于清除数据库, 两者的区别的是 flushdb 只清除当前数据库, flushall 会清除所有数据库。 diff --git a/spring/spring-scheduling-annotation/README.md b/spring/spring-scheduling-annotation/README.md index 7111b9e..fe7dca3 100644 --- a/spring/spring-scheduling-annotation/README.md +++ b/spring/spring-scheduling-annotation/README.md @@ -1,102 +1,74 @@ -# spring 定时任务(注解方式) +# Spring 定时任务(注解方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring scheduling
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 - -关于任务的调度配置定义在 ServletConfig.java 中,为方便观察项目定时执行的情况,项目以 web 的方式构建。 +关于任务的调度配置定义在 ServletConfig 中,为方便观察定时执行的情况,项目以 web 的方式构建。
+### 1.2 基本依赖 -### 1.2 依赖说明 - -导入基本依赖 +导入基本依赖: ```xml - - - 4.0.0 + + 5.1.3.RELEASE + - com.heibaiying - spring-scheduling - 1.0-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - - - 5.1.3.RELEASE - - - - - org.springframework - spring-context - ${spring-base-version} - - - org.springframework - spring-beans - ${spring-base-version} - - - org.springframework - spring-core - ${spring-base-version} - - - org.springframework - spring-web - ${spring-base-version} - - - org.springframework - spring-webmvc - ${spring-base-version} - - - javax.servlet - javax.servlet-api - 4.0.1 - provided - - - - + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + ``` -## 二、spring scheduling +## 二、Spring Scheduling -#### 2.1 创建定时任务 +### 2.1 创建定时任务 ```java -/** - * @author : heibaiying - */ @Component public class Task { @@ -132,13 +104,9 @@ public class Task { ``` -#### 2.2 配置定时任务 +### 2.2 配置定时任务 ```java -/** - * @author : heibaiying - * spring 主配置类 - */ @Configuration @EnableWebMvc @EnableScheduling //启用 Spring 的计划任务执行功能 @@ -194,9 +162,8 @@ public class ServletConfig implements WebMvcConfigurer, AsyncConfigurer, Schedul **关于调度程序线程池作用说明**: -按照例子 我们有 methodA 、 methodB 、methodC 三个方法 其中 methodB 是耗时的方法如果不声明调度程序线程池 则 methodB 会阻塞 methodA 、methodC 方法的执行 因为调度程序是单线程的 +如上 methodA 、 methodB 、methodC 三个方法,其中 methodB 是耗时方法,如果不声明调度程序线程池则 methodB 会阻塞 methodA 、methodC 方法的执行 因为调度程序是单线程的。 **关于任务执行线程池作用说明**: -按照例子 如果我们声明 methodB 是按照 fixedRate=5000 方法执行的 ,理论上不管任务耗时多久,任务都应该是每 5 秒执行一次,但是实际上任务是被加入执行队列,也不会立即被执行,因为默认执行任务是单线程的,这个时候需要开启@EnableAsync 并指定方法是 @Async 异步的,并且配置执行任务线程池 (如果不配置就使用默认的线程池配置) - +按照上面的例子,如果我们声明 methodB 是按照 fixedRate=5000 方法执行的 ,理论上不管任务耗时多久,任务都应该是每 5 秒执行一次,但是实际上任务是被加入执行队列,并不会立即被执行,因为默认执行任务是单线程的。这个时候需要开启@EnableAsync 注解并指定方法是 @Async 异步的,并且配置执行任务线程池 (如果不配置就使用默认的线程池配置)。 \ No newline at end of file diff --git a/spring/spring-scheduling/README.md b/spring/spring-scheduling/README.md index edf958e..e9ccb5d 100644 --- a/spring/spring-scheduling/README.md +++ b/spring/spring-scheduling/README.md @@ -1,109 +1,75 @@ -# spring 定时任务(xml配置方式) +# Spring 定时任务(XML 配置方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring scheduling
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 - -关于任务的调度配置定义在 springApplication.xml 中,为方便观察项目定时执行的情况,项目以 web 的方式构建。 +关于任务的调度配置定义在 `springApplication.xml` 中,为方便观察定时执行的情况,项目以 web 的方式构建。
+### 1.2 基本依赖 -### 1.2 依赖说明 - -导入基本依赖 +导入基本依赖: ```xml - - - 4.0.0 + + 5.1.3.RELEASE + - com.heibaiying - spring-scheduling - 1.0-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - - - 5.1.3.RELEASE - + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + - - - org.springframework - spring-context - ${spring-base-version} - - - org.springframework - spring-beans - ${spring-base-version} - - - org.springframework - spring-core - ${spring-base-version} - - - org.springframework - spring-web - ${spring-base-version} - - - org.springframework - spring-webmvc - ${spring-base-version} - - - javax.servlet - javax.servlet-api - 4.0.1 - provided - - - - ``` -## 二、spring scheduling +## 二、Spring Scheduling -#### 2.1 创建定时任务 +### 2.1 创建定时任务 ```java -package com.heibaiying.task; - -import org.springframework.scheduling.annotation.Async; -import org.springframework.scheduling.annotation.Scheduled; - -import java.time.LocalDateTime; - -/** - * @author : heibaiying - */ public class Task { public void methodA() { @@ -119,17 +85,16 @@ public class Task { thread.getName(), thread.getId(), "methodB 方法执行", LocalDateTime.now())); Thread.sleep(10 * 1000); } - + public void methodC() { Thread thread = Thread.currentThread(); System.out.println(String.format("线程名称:%s ; 线程 ID:%s ; 调用方法:%s ; 调用时间:%s", thread.getName(), thread.getId(), "methodC 方法执行", LocalDateTime.now())); } } - ``` -#### 2.2 配置定时任务 +### 2.2 配置定时任务 ```xml @@ -185,9 +150,9 @@ public class Task { **关于调度程序线程池作用说明**: -按照例子 我们有 methodA 、 methodB 、methodC 三个方法 其中 methodB 是耗时的方法如果不声明调度程序线程池 则 methodB 会阻塞 methodA 、methodC 方法的执行 因为调度程序是单线程的 +如上 methodA 、 methodB 、methodC 三个方法,其中 methodB 是耗时方法,如果不声明调度程序线程池则 methodB 会阻塞 methodA 、methodC 方法的执行 因为调度程序是单线程的。 **关于任务执行线程池作用说明**: -按照例子 如果我们声明 methodB 是按照 fixedRate=5000 方法执行的 ,理论上不管任务耗时多久,任务都应该是每 5 秒执行一次,但是实际上任务是被加入执行队列,也不会立即被执行,因为默认执行任务是单线程的,这个时候需要开启@EnableAsync 并指定方法是 @Async 异步的,并且配置执行任务线程池 (如果不配置就使用默认的线程池配置) +按照上面的例子,如果我们声明 methodB 是按照 fixedRate=5000 方法执行的 ,理论上不管任务耗时多久,任务都应该是每 5 秒执行一次,但是实际上任务是被加入执行队列,并不会立即被执行,因为默认执行任务是单线程的。这个时候需要开启@EnableAsync 注解并指定方法是 @Async 异步的,并且配置执行任务线程池 (如果不配置就使用默认的线程池配置)。 diff --git a/spring/spring-websocket-annotation/README.md b/spring/spring-websocket-annotation/README.md index 62cde78..1128c7c 100644 --- a/spring/spring-websocket-annotation/README.md +++ b/spring/spring-websocket-annotation/README.md @@ -1,36 +1,34 @@ -# spring websocket(注解方式) +# Spring WebSocket(注解方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring websocket
-        2.1 创建消息处理类,继承自TextWebSocketHandler
-        2.2 创建websocket 握手拦截器(如果没有权限拦截等需求,这一步不是必须的)
-        2.3 创建websocket的配置类
-        2.4 前端 websocket 的实现
-        2.5 简单登录的实现
-## 正文
+ +## 一、项目说明 -## 一、说明 +### 1.1 项目结构 -### 1.1 项目结构说明 - -1. 项目模拟一个简单的群聊功能,为区分不同的聊天客户端,登录时候将临时用户名存储在 session 当中; -2. webconfig 包是基础注解的方式配置 web,在 spring-base-annotation 项目中已经讲解过每个类作用; -3. CustomHander 为消息的自定义处理器; -4. CustomHandershakerInterceptor 为自定义的 websocket 的握手拦截器; -5. webSocketConfig 是 websocket 的主要配置类; -6. 项目以 web 的方式构建。 +- 项目模拟一个简单的群聊功能,为区分不同的聊天客户端,登录时候将临时用户名存储在 session 当中; +- webconfig 包是基础注解的方式配置 web,在 spring-base-annotation 项目中已经讲解过每个类作用; +- CustomHander 为消息的自定义处理器; +- CustomHandershakerInterceptor 为自定义的 websocket 的握手拦截器; +- webSocketConfig 是 websocket 的主要配置类; +- 项目以 web 的方式构建。
+### 1.2 基本依赖 -### 1.2 依赖说明 - -除了基本的 spring 依赖外,还需要导入 webSocket 的依赖包 +除了基本的 Spring 依赖外,还需要导入 WebSocket 的依赖: ```xml @@ -43,13 +41,14 @@ -## 二、spring websocket +## 二、实现 WebSocket -#### 2.1 创建消息处理类,继承自TextWebSocketHandler +### 2.1 消息处理器 + +继承自TextWebSocketHandler,创建消息处理类: ```java /** - * @author : heibaiying * @description : 自定义消息处理类 */ public class CustomHandler extends TextWebSocketHandler { @@ -98,11 +97,12 @@ public class CustomHandler extends TextWebSocketHandler { ``` -#### 2.2 创建websocket 握手拦截器(如果没有权限拦截等需求,这一步不是必须的) +### 2.2 握手拦截器 + +创建websocket 握手拦截器(如果没有权限拦截等需求,这一步不是必须的): ```java /** - * @author : heibaiying * @description : 可以按照需求实现权限拦截等功能 */ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor { @@ -123,13 +123,9 @@ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor } ``` -#### 2.3 创建websocket的配置类 +### 2.3 配置 WebSocket ```java -/** - * @author : heibaiying - * @description :websocket 配置 - */ @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @@ -141,7 +137,7 @@ public class WebSocketConfig implements WebSocketConfigurer { } ``` -#### 2.4 前端 websocket 的实现 +### 2.4 前端 WebSocket ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> @@ -185,9 +181,9 @@ public class WebSocketConfig implements WebSocketConfigurer { ``` -#### 2.5 简单登录的实现 +### 2.5 实现简单登录 -```java +```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> diff --git a/spring/spring-websocket/README.md b/spring/spring-websocket/README.md index fbb5935..926537e 100644 --- a/spring/spring-websocket/README.md +++ b/spring/spring-websocket/README.md @@ -1,37 +1,35 @@ -# spring websocket(xml配置方式) +# Spring WebSocket(XML 配置方式) -## 目录
-一、说明
-    1.1 项目结构说明
-    1.2 依赖说明
-二、spring websocket
-        2.1 创建消息处理类,继承自TextWebSocketHandler
-        2.2 创建websocket 握手拦截器(如果没有权限拦截等需求,这一步不是必须的)
-        2.3 配置websocket
-        2.4 前端 websocket 的实现
-        2.5 简单登录的实现
-## 正文
+ +## 一、项目说明 -## 一、说明 - -### 1.1 项目结构说明 +### 1.1 项目结构 1. 项目模拟一个简单的群聊功能,为区分不同的聊天客户端,登录时候将临时用户名存储在 session 当中; -2. CustomHander 为消息的自定义处理器; +2. CustomHander 为自定义的消息处理器; 3. CustomHandershakerInterceptor 为自定义的 websocket 的握手拦截器; 4. 项目以 web 的方式构建。
+### 1.2 基本依赖 -### 1.2 依赖说明 - -除了基本的 spring 依赖外,还需要导入 webSocket 的依赖包 +除了基本的 Spring 依赖外,还需要导入 WebSocket 的相关依赖: ```xml - + org.springframework spring-websocket @@ -41,13 +39,14 @@ -## 二、spring websocket +## 二、实现 WebSocket -#### 2.1 创建消息处理类,继承自TextWebSocketHandler +### 2.1 消息处理器 + +继承自TextWebSocketHandler,创建消息处理类: ```java /** - * @author : heibaiying * @description : 自定义消息处理类 */ public class CustomHandler extends TextWebSocketHandler { @@ -96,11 +95,12 @@ public class CustomHandler extends TextWebSocketHandler { ``` -#### 2.2 创建websocket 握手拦截器(如果没有权限拦截等需求,这一步不是必须的) +### 2.2 握手拦截器 + +创建websocket 握手拦截器(如果没有权限拦截等需求,这一步不是必须的): ```java /** - * @author : heibaiying * @description : 可以按照需求实现权限拦截等功能 */ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor { @@ -121,7 +121,7 @@ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor } ``` -#### 2.3 配置websocket +### 2.3 配置 WebSocket ```xml @@ -171,7 +171,7 @@ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor ``` -#### 2.4 前端 websocket 的实现 +### 2.4 前端 WebSocket ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> @@ -215,9 +215,9 @@ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor ``` -#### 2.5 简单登录的实现 +### 2.5 实现简单登录 -```java +```JSP <%@ page contentType="text/html;charset=UTF-8" language="java" %> diff --git a/spring/springmvc-base-annotation/README.md b/spring/springmvc-base-annotation/README.md index 466479d..61272df 100644 --- a/spring/springmvc-base-annotation/README.md +++ b/spring/springmvc-base-annotation/README.md @@ -1,6 +1,5 @@ # Spring MVC 基础(基于注解) + ## 一、搭建 Hello Spring 工程 ### 1.1 项目搭建 @@ -471,7 +473,7 @@ public class Programmer { ## 六、文件上传与下载 -#### 6.1 文件上传 +### 6.1 文件上传 1.在 ServletConfig 中进行配置,开启文件上传: @@ -613,7 +615,7 @@ public class FileUtil { ``` -#### 6.2 文件下载 +### 6.2 文件下载 1.在 fileController.java 中增加下载方法: diff --git a/spring/springmvc-base/README.md b/spring/springmvc-base/README.md index ffc3374..a7010dd 100644 --- a/spring/springmvc-base/README.md +++ b/spring/springmvc-base/README.md @@ -1,4 +1,4 @@ -# Spring MVC 基础(基于 Xml 配置) +# Spring MVC 基础(基于 XML 配置) + + ## 一、搭建 Hello Spring 工程 ### 1.1 构建 Web 项目 @@ -505,7 +509,7 @@ public class Programmer { ## 六、文件上传与下载 -#### 6.1 文件上传 +### 6.1 文件上传 1.在 `springApplication.xml` 中进行配置,开启文件上传: @@ -647,7 +651,7 @@ public class FileUtil { ``` -#### 6.2 文件下载 +### 6.2 文件下载 1.在 fileController.java 中增加下载方法: