前言
继续进行整合
Service 层的配置
spring-service 配置
1 | /resources/spring/spring-service.xml |
在上述文件中配置 Service 层。
主要有三步:
- 扫描 service 包下所有使用注解的类型
- 配置事务管理器,并注入数据库的连接池
- 配置基于注解的声明式事务,默认使用注解来管理事务行为
采用注解来管理事务行为,自由选择事务开启,提高性能,减少开启事务开销。
代码如下:
1 | <?xml version="1.0" encoding="UTF-8"?> |
Service 类代码
在 Service 包里面建立 Service 类,在类级别使用 @Service 注解。DAO 的类则使用 @Autowired 类,需要开启事务的方法则 使用 @Transactional 注解。
比如下: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
64package org.seckill.service.impl;
import 此处省略
/**
* Created by ARNO on 2016/6/6/006.
*/
public class SeckillServiceImpl implements SeckillService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private SeckillDao seckillDao;
private SuccessKilledDao successKilledDao;
private RedisDao redisDao;
// MD5 字符串,用于混淆代码
private final String slat = "DFG$%^$%^YH45r7dfgdfgdfY#$%GBERH&^*OL>:";
/**
* 查询所有秒杀记录
*
* @return
*/
public List<Seckill> getSeckillList() {
return seckillDao.queryAll(0, 4);
}
/**
* 查询单个秒杀记录
*
* @param seckillId
* @return
*/
public Seckill getById(long seckillId) {
return seckillDao.queryById(seckillId);
}
/**
* 执行秒杀操作
*
* @param seckillId
* @param userPhone
* @param md5
*
* 使用注解控制事务方法的优点:
* 1、开发团队达成一致约定,明确标注事务方法的编程风格
* 2、保证事务方法的执行时间尽可能短,不要穿插其他网络操作(RPC/HTTP请求)。可以剥离到事务方法外部。
* 3、不是所有的方法都需要事务,如只有一条修改操作,只读操作不需要事务控制。
*/
public SeckillExecution executeSekill(long seckillId, long userPhone, String md5)
throws SeckillException, SeckillCloseException, RepeatKillException {
此处省略
}
}
Service 类接口
1 | package org.seckill.service; |
整合Spring MVC
spring-web.xml 配置
1 | /resources/spring/spring-web.xml |
在上述文件中配置 web 层。
主要有四步:
- 开启 SpringMVC 注解模式
- 静态资源默认 servlet 配置
- 配置 jsp 显示 ViewResolver
- 扫描 Web 相关的 bean
1 | <?xml version="1.0" encoding="UTF-8"?> |
此处有一个坑,Windows 下使用 IntelliJ IDEA 2016.1.2(64) 版本,把 xmlns:mvc 配置成 cache,导致报错,No bean named ‘cacheManager’ is defined。
web.xml 配置
1 | /WEB-INF/web.xml |
在上述文件中配置web.xml
代码如下: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<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<!--修改 servlet 版本为 3.1 -->
<!--配置 DispatcherServlet-->
<servlet>
<servlet-name>seckill-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置 SpringMVC 需要加载的配置的文件:spring-dao.xml,spring-service.xml,spring-web.xml
MyBatis -> Srping -> SpringMVC
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>seckill-dispatcher</servlet-name>
<!--默认匹配所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Controller 类代码
在类界别使用 @Controller 注解,@RequestMapping 注解绑定 URL 路径。
代码范例如下:
1 | package org.seckill.web; |
友好的 Restful 设计范例
1 | URL 设计 |