0%

1.需要分离出来的类

  • 实体类
  • dao层, 直接在server端实现.

OSS 封装为接口 阿里云 与 七牛云
阿里云方法:

  • OSSClient getOssClient();
  • boolean createBucket();
  • boolean deleteBucket();
  • String updateFile();
  • InputStream getFile();
  • boolean deleteFile();

七牛云

  • Auth getAuth();
  • Configuration getCfg():
  • UploadManager getUploadManager();
  • BucketManager getBucketManager();
  • boolean delete(String keyFile)
  • boolean updateFile(Integer id, MultipartFile multipartFile)
  • BucketManager.FileListIterator getObjectList
阅读全文 »

读取步骤

  1. 读取

    • 读取数据库数据
    • 成功 -> 写入缓存
    • 失败 -> 不写入缓存
  2. 更新

    • 更新数据库数据
    • 成功 -> 将缓存标记失败, 不要写入缓存.

      两个并发操作,一个是更新操作,另一个是查询操作,更新操作删除缓存后,查询操作没有命中缓存,先把老数据读出来后放到缓存中,然后更新操作更新了数据库。

  3. 插入

    • 插入数据库
    • 成功 -> 不写入缓存.

刷新内存,以memcached为例,新增,修改和删除操作,一般采用lazy load的策略,即新增时只写入数据库,并不会马上更新Memcached,而是等到再次读取时才会加载到Memcached中,修改和删除操作也是更新 数据库,然后将Memcached中的数据标记为失效,等待下次读取时再加载。

Java

后端接收文件:

1
2
3
4
5
@RequestMapping(value = "/uploaded")
@ResponseBody
public String uploaded(MultipartFile file){
return file.getOriginalFilename();
}

posoman 测试

注意不要请求头

可能会遇到的错误

阅读全文 »

今日完成的事

  • 线上项目 整合Cookie 登陆
  • 通过 Memcached 来同步Session, 解决tomcat多开带来的Session同步问题.

Memcached 同步 Session

  1. 由于使用了多台tomcat, 在Nginx做负载均衡时会将请求随机分发到各个tomcat上, 当属于tomcat_1的登陆后会话请求被转发到tomcat_2上时, 就会出现登陆验证错误, 因为tomcat_2上这个会话并未登陆.

  2. 解决方法, 通过Memcache保存Session会话.

    • 注意, Session是容器保存的,内容放置在服务器具体的文件中,并不是程序保存的, 这个概念一定要搞清楚.
    • 我这里场景是 登陆后执行一个请求列出所有用户, 所以不能直接转到jsp.
    • 使用 redirect 跳转页面会导致 session 值丢失. 参考连接1 参考连接2
    • 解决方法
      1. 思路: handler中保存session后,先forward到一个handler再redirect到目标handler, session值不会丢失
      2. 具体实现:
        • handler 中保存 session, 此时session还未保存到容器中
          • 返回页面使用redirect到新页面,因为重定向特性不会带前页面的数据, 所以容器会将session值直接丢弃,导致session值丢失.(session id还在)
          • 返回时使用 forward 到新handler, 容器会将session保存.
        • 在新handler中再redirect到新页面, 此时session已保存,不会影响

    将Tomcat的session由Memcached保存 准备工作

  • Nginx 负载均衡已配置
  • Tomcat 已开启多个容器
  • Tomcat 8
  • 官方文档: https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
  • 参考资料: https://www.cnblogs.com/jsonhc/p/7344902.html
  • 下载所需jar包, 每个tomcat版本对应的包不同

  • wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.1.1/memcached-session-manager-2.1.1.jar
  • wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/2.1.1/memcached-session-manager-tc8-2.1.1.jar
  • wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar
  • 如果仅仅只是用java来做序列化器只需上面这三个包就ok
  • 下面两个包,配置后一直报错,未测试通过
  • wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/1.8.3/msm-javolution-serializer-1.8.3.jar
  • wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/2.1.1/msm-javolution-serializer-2.1.1.jar
  • 下载完成后放入tomcat/lib目录中

修改两个tomcat配置文件,启动

阅读全文 »

今天完成的事情

  • 完成Spring MVC memcachede 融合

整合

  1. memcachede只缓存数据, 所以我们只需要在service 层 的实现中添加对应的缓存即可.
  • 原接口添加缓存
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    @Override
    public UserCustom findUserById(Integer id) throws Exception {
    // 查找缓存
    Object object = MemcacheUtils.get("user" + id);
    // 当存在缓存时直接返回缓存数据
    if (object != null) {
    return (UserCustom) object;
    }
    UserCustom userCustom = userDao.findUserById(id);
    // 当缓存为空时 添加 memcached 缓存
    MemcacheUtils.set("user" + id, userCustom);
    return userCustom;
    }

    @Override
    public int insertUser(User user) throws Exception {
    //插入成功后返回的值存入了user的id中
    userDao.insertUser(user);
    // 写入缓存 这里使用add 当 key(id)存在时, 不写入缓存
    MemcacheUtils.add("user" + user.getId(), user);
    //所以返回user的id值
    return user.getId();
    }

    @Override
    public boolean updateUser(UserCustom userCustom, Integer id) throws Exception {
    userCustom.setId(id);
    // 写入缓存 这里使用replace, 当key(id)不存在时, 不写入缓存
    MemcacheUtils.replace("user" + id, userCustom);
    return userDao.updateUser(userCustom);
    }

    @Override
    public boolean deleteUser(Integer i) throws Exception {
    // 删除缓存
    MemcacheUtils.delete(String.valueOf(i));
    return userDao.deleteUser(i);
    }

    @Override
    public boolean findAuth(Auth auth) throws Exception {
    // 密码验证的就不做缓存了
    return authDao.findAuth(auth);
    }

  • 测试 查找数据, 第一次从数据库中查询并将值存入到缓存, 第二次直接从缓存获取
    image
    image
    image
  • 测试 更新数据, 再次获取, 直接从缓存中获取更新过的数据
    image
    image
  1. 创建缓存api接口
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    package com.jnshu.controller;

    import com.jnshu.model.UserCustom;
    import com.jnshu.service.UserService;
    import com.jnshu.tools.MemcacheUtils;
    import com.whalin.MemCached.MemCachedClient;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.*;


    /**
    * @program: taskTwo
    * @description: MemCache 缓存接口
    * @author: Mr.xweiba
    * @create: 2018-05-19 00:06
    **/

    @Controller
    @RequestMapping("/memcache")
    public class MemCacheController {
    @Autowired
    UserService userService;
    private static Logger logger = LoggerFactory.getLogger(MemCacheController.class);

    /**
    * @Description: 获取key为id的user缓存
    * @Param: [key]
    * @return: java.lang.Object
    * @Author: Mr.Wang
    * @Date: 2018/5/19
    */
    @RequestMapping(value = "/api/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object findByKey(@RequestBody @PathVariable("id") String key){
    if(StringUtils.isEmpty(key)){
    return "key must not be empty or null!";
    }
    return MemcacheUtils.get("user" + key);
    }

    /**
    * @Description: 缓存更新接口 当key不存在时取消更新
    * @Param: [key, userCustom] 键. 值
    * @return: boolean
    * @Author: Mr.Wang
    * @Date: 2018/5/19
    */
    @RequestMapping(value = "/api/{id}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public boolean updateByKey(@PathVariable("id") String key, @RequestBody UserCustom userCustom){
    userCustom.setId(Integer.valueOf(key));
    if(StringUtils.isEmpty(key)){
    return false;
    }
    return MemcacheUtils.replace("user" + key, userCustom);
    }

    /**
    * @Description: 增加缓存数据 当键存在时取消存入
    * @Param: [key, userCustom] 键, 值
    * @return: java.lang.Boolean
    * @Author: Mr.Wang
    * @Date: 2018/5/19
    */
    @RequestMapping(value = "/api/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    @ResponseBody
    public Boolean insert(@PathVariable("id") String key, @RequestBody UserCustom userCustom){
    System.out.println(userCustom.toString());
    userCustom.setId(Integer.valueOf(key));
    if(StringUtils.isEmpty(key)){
    return false;
    }
    return MemcacheUtils.add("user" + key, userCustom);
    }

    /**
    * @Description: 删除指定key
    * @Param: [key]
    * @return: java.lang.Boolean
    * @Author: Mr.Wang
    * @Date: 2018/5/19
    */
    @RequestMapping(value = "/api/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Boolean deleteByKey(@PathVariable("id") String key){
    if(StringUtils.isEmpty(key)){
    return false;
    }
    return MemcacheUtils.delete("user" + key);
    }
    /**
    * @Description: 清除缓存中的所有键值对
    * @Param: []
    * @return: boolean
    * @Author: Mr.Wang
    * @Date: 2018/5/19
    */
    @RequestMapping(value = "/api/all", method = RequestMethod.DELETE)
    @ResponseBody
    public boolean flashAll() throws Exception {
    return MemcacheUtils.flashAll();
    }
    }

阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 1984组 求学大作战后台服务 academy_canary_admin_web
# 开发环境配置
# Spring 设置
spring:
datasource:
# 连接池
type: com.alibaba.druid.pool.DruidDataSource
# 数据库驱动
driver-class-name: com.mysql.jdbc.Driver
# 数据库url
url: jdbc:mysql://localhost:3306/canary?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
username: test
password: test123456
application:
name: academy-canary
# HTTP ENCODING
http:
multipart:
max-file-size: 2MB
max-request-size: 10MB
encoding:
enabled: true
charset: UTF-8
force: true
messages:
encoding: UTF-8
jmx:
enabled: true
default-domain: agentservice
resources:
chain:
strategy:
content:
enabled: true
paths: /**
server:
context-path: /a
port: 20475
# HTTP请求和响应头的最大量, 以字节为单位, 默认值为4096, 超过此长度的部分不予处理, 一般为8k. 解决java.io.EOFException: null问题
max-http-header-size: 8192
# 请求是否允许X-Forwarded-*
use-forward-headers: true
compression:
# 是否开启压缩,默认为false.
enabled: true
# 执行压缩的阈值,默认为2048
min-response-size: 1024
# 指定要压缩的MIME type,多个以逗号分隔.
mime-types: text/plain,text/css,text/xml,text/javascript,application/json,application/javascript,application/xml,application/xml+rss,application/x-javascript,application/x-httpd-php,image/jpeg,image/gif,image/png
tomcat:
# 设定remote IP的header,如果remoteIpHeader有值,则设置为RemoteIpValve
remote-ip-header: X-Forwarded-for
# 设定Header包含的协议,通常是 X-Forwarded-Proto,如果remoteIpHeader有值,则将设置为RemoteIpValve.
protocol-header: X-Forwarded-Proto
# 设定http header使用的,用来覆盖原来port的value.
port-header: X-Forwarded-Port
# 设定URI的解码字符集.
uri-encoding: UTF-8
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)
basedir: /var/tmp/website-app

# MyBatis
mybatis:
# 多个包加 ,
type-aliases-package: com.ptteng.academy.persistence.beans,com.ptteng.academy.business.dto,com.ptteng.academy.business.query
mapper-locations: classpath:/mybatis/*.xml
# mapper
mapper:
mappers:
- com.ptteng.academy.plugin.BaseMapper
not-empty: false
identity: MYSQL
# pagehelper
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
banner:
charset: UTF-8

canary:
druid:
# druid访问用户名(默认:test)
username: test
# druid访问密码(默认:zyd-druid)
password: test
# druid访问地址(默认:/druid/*)
servletPath: /druid/*
# 启用重置功能(默认false)
resetEnable: false
# 白名单(非必填), list
allowIps[0]:
# 黑名单(非必填), list
denyIps[0]:

# 开启mysql sql 日志
logging:
level:
com.ptteng.academy.persistence.mapper: DEBUG

springboot 导入外部配置, core定义全局配置:

1
2
3
4
spring:
profiles:
# 引入核心配置
include: core

全局版本号

1
2
3
4
5
6
7
8
<properties>
<!-- 设置编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.14.RELEASE</spring.version>
<!--log4j日志包版本号-->
<slf4j.version>1.7.18</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<dependencies>
<!-- redis -->
<!-- redis 连接驱动-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring 支持包 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<!-- redis end -->
<!-- memcached -->
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.2</version>
</dependency>
<!-- end -->
<!-- 对象池 -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<!-- end -->
<!-- encodebase64string -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<!-- end -->
<!-- Tiles 包-->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>2.2.1</version>
</dependency>
<!-- Tiles end -->

<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- mysql end -->
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- junit end -->

<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatis-spring组合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mybatis end-->

<!-- 上传文件使用 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 上传文件使用end-->

<!-- 加载json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 加载json end -->

<!-- validator 数据验证-->
<!--jsr 303-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- hibernate validator-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- validator 数据验证end-->
<!-- 日志 -->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.0.Final</version>
</dependency>
<!-- 日志end-->


<!-- jstl -->
<!-- HttpServletRequest http请求依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 只在编译和测试阶段运行,因为容器中自带了这个jar包 jetty中自带 -->
<!--<scope>provided</scope>-->
</dependency>
<!-- HttpServletRequest http请求依赖 end -->

<!-- web el表达式 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- web el表达式 end -->

<!-- 添加taglibs标签包 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 添加taglibs标签包end -->

<!-- jstl end-->

<!-- slf4j日志包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- slf4j end -->

<!-- spring start -->
<!-- spring dbcp连接jar包-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<!-- 自动生成sql-->
<!-- @Query("update #{#entityName} rule set rule.isEnable=1,rule.lastEnable= NOW() where rule.id= :id")
@Modifying
public int enable(@Param("id")int id);
-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
<!-- spring aop包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- aop 注解包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<!-- spring aop包end -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
</dependencies>

插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.5.v20170502</version>
<configuration>
<!--开启自动检测配置,默认为0,不改变. 单位为秒 -->
<scanIntervalSeconds>2</scanIntervalSeconds>
<httpConnector>
<!--配置端口号-->
<port>8080</port>
<!--空闲超时-->
<idleTimeout>60000</idleTimeout>
</httpConnector>
<!--解决静态文件修改后不刷新的问题-->
<!--原因是如果NIO被支持的话,Jetty会使用内存映射文件来缓存静态文件,其中包括.js文件。在Windows下面,使用内存映射文件会导致文件被锁定。解决方案是不使用内存映射文件来做缓存。-->
<!--<webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>-->
</configuration>
</plugin>

<!-- 自动部署到服务器 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port> <!-- 项目的端口-->
<path>/taskTwo</path> <!-- web项目的项目名称-->
<uriEncoding>UTF-8</uriEncoding>
<!-- 对应的 tomcat manager的接口-->
<!-- 本地测试 -->
<!--<url>http://localhost:8080/manager/text</url>-->
<!-- 远程测试 -->
<url>http://45.76.214.173:8080/manager/text</url>
<server>tomcat7</server> <!-- setting.xml 的server id-->
<username>tomcat</username> <!-- tomcat-user.xml 的 username -->
<password>managerstatus</password> <!-- tomcat-user,xml 的 password -->
</configuration>
</plugin>
阅读全文 »

@SuppressWarnings


用于抑制编译器产生警告信息。

  • 示例1——抑制单类型的警告
    1
    2
    3
    4
    5
    6
    @SuppressWarnings("unchecked")
    public void addItems(String item){
    @SuppressWarnings("rawtypes")
    List items = new ArrayList();
    items.add(item);
    }
  • 示例2——抑制多类型的警告
    1
    2
    3
    4
    5
    @SuppressWarnings(value={"unchecked", "rawtypes"})
    public void addItems(String item){
    List items = new ArrayList();
    items.add(item);
    }
  • 示例3——抑制所有类型的警告
    1
    2
    3
    4
    5
    @SuppressWarnings("all")
    public void addItems(String item){
    List items = new ArrayList();
    items.add(item);
    }
    关键字 用途
    all 抑制所有警告
    boxing 抑制与箱/非装箱操作相关的警告
    cast 抑制与铸造操作相关的警告
    dep-ann 相对于弃用的注释,抑制警告
    deprecation 抑制相对于弃用的警告
    fallthrough 在switch语句中,相对于缺失的中断,抑制警告
    finally 抑制警告相对于最终阻止不返回的警告告
    hiding 抑制相对于隐藏变量的局部的警告
    incomplete-switch 为了在switch语句(enum案例)中抑制相对于缺失条目的警告告
    nls 要抑制相对于非nls字符串字面量的警告
    null 抑制null相关的警告
    rawtypes 在类params上使用泛型时,抑制相对于非特异性类型的警告
    serial 针对可串行化类,抑制相对于缺少serialVersionUID字段的警告
    static-access 抑制不正确的静态访问的警告
    synthetic-access 抑制相对于内部类的未优化访问的警告
    unchecked 抑制相对于未检查操作的警告
    unqualified-field-access 禁止与字段访问不合格的警告
    unused 抑制相对于弃用的警告
    deprecation 抑制未使用的代码的警告

@PathVariable

  • 当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
阅读全文 »

Maven 包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- redis -->
<!-- redis 驱动-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring redis数据支持包 spring-data-redis 2.0后版本只支持spring5和spring boot2 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
<!-- redis end -->

依赖冲突

  1. 在mavne仓库里面,查找你的jar相关的版本

    地址: http://mvnrepository.com/
    点开相关版本,查看Compile Dependencies 列表,会显示此包所依赖的相关版本信息.

  2. spring-data-redis依赖

    其最低的spring依赖版本为4.3.10.RELEASE,将spring版本升级到到4.3.10

  3. 升级spring版本后AOP的依赖又出现了问题. 心累

    报错异常 :
    Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry

  4. 原因是spring 4.3.10以上 aspectJ 版本必须高于1.8.9, 我升级到1.8.11正常.

配置

  1. redis连接配置 redis.properties

    redis.host=127.0.0.1
    redis.port=6379
    redis.pass=
    redis.maxIdle=300
    redis.maxWait=1000
    redis.testOnBorrow=true

  2. 添加redis spring配置 `applicationContenxt-redis.xml

    • 导入redis连接配置

      1
      <context:property-placeholder location="WEB-INF/config/other/redis.properties"/>
    • 报错 Could not resolve placeholder 参考连接

      因为使用了多个PropertyPlaceholderConfigurer或者多个context:property-placeholder的原因。
      一定要记住,不管是在一个Spring文件还是在多个Spring文件被统一load的情况下,直接写多个都是不允许的.

    • 解决方法

      1. 解决方法一, 定义一个全局context导入不同配置
        1
        2
        3
        4
        5
        6
        7
        <context-param>  
        <param-name>contextConfigLocation</param-name>
        <param-value>
        WEB-INF/config/spring/dao.xml,
        WEB-INF/config/spring/dfs.xml
        </param-value>
        </context-param>
      2. 解决方法二, 每资源导入时都要加上ignore-unresolvable="true"参数,一个加另一个不加也是不行的
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
           <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>  
        <context:property-placeholder location="yyy.properties" ignore-unresolvable="true" />
        - 注入连接池配置 [testOnBorrow作用](https://blog.csdn.net/wangyangzhizhou/article/details/52209336)
        ```xml
        <!-- 注入连接池配置 -->
        <bean id="redis-config" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲数 -->
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <!-- 最大等待时间 -->
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <!-- 是否检测连接池的可用性 -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        </bean>
        参考连接
阅读全文 »