首页 > 代码库 > Java高并发秒时啊API之Service层1
Java高并发秒时啊API之Service层1
---2-1 使用Spring托管Service依赖理论----------------------------
spring ioc优势(工厂模式):
1.对象创建统一托管
2.规范的生命周期管理
3.灵活的依赖注入
4.一致的获取对象
Spring IOC 功能的理解
DAO依赖+Service依赖最终形成一致访问接口;
随意访问依赖对象
Spring IOC 容器下的 对象 默认都是单例的。
业务对象依赖图:Spring IOC容器 通过 DI 解决 依赖链(对象之间的依赖关系)
SeckillService ->SeckillDao ->SqlSessionFactry->DataSource...
->successKilledDao
Spring-IOC注入方式和场景
本项目IOC使用:
XML配置
package-scan
Annotation注解
---2-2 使用Spring托管Service依赖配置---------------------------------------------------------
SeckillServiceImpl.java:
配置spring-service.xml
标注注解@Service和@Autowired
package org.seckill.service.impl; import java.util.Date; import java.util.List; import org.seckill.dao.SeckillDao; import org.seckill.dao.SuccessKilledDao; import org.seckill.dto.Exposer; import org.seckill.dto.SeckillExecution; import org.seckill.entity.Seckill; import org.seckill.entity.SuccessKilled; import org.seckill.enums.SeckillStatEnum; import org.seckill.exception.RepeatKillExeception; import org.seckill.exception.SeckillCloseException; import org.seckill.exception.SeckillException; import org.seckill.service.SeckillService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.DigestUtils; //@Component @service @Dao @Controller @Service public class SeckillServiceImpl implements SeckillService { private Logger logger = LoggerFactory.getLogger(this.getClass()); //注入Service依赖 @Autowired //在Spring 查找Dao类型的实例(Mybatis实现的,),并注入。不在需要自己new 实例 //还有@Resource,@Inject等J2EE规范的注解 private SeckillDao seckillDao; @Autowired private SuccessKilledDao successKilledDao; //md5盐值字符串,用于混淆MD5 private final String slat = "sldijfldkjfpaojj@#(#$sldfj`123"; @Override public List<Seckill> getSeckillList() { return seckillDao.queryAll(0, 4); } @Override public Seckill getById(long seckillId) { // TODO Auto-generated method stub return seckillDao.queryById(seckillId); } @Override public Exposer exportSeckillUrl(long seckillId) { Seckill seckill = seckillDao.queryById(seckillId); if(seckill == null){ return new Exposer(false,seckillId); } Date startTime = seckill.getStartTime(); Date endTime = seckill.getEndTime(); //系统当前时间 Date nowTime = new Date(); if(nowTime.getTime() < startTime.getTime() || nowTime.getTime() > endTime.getTime()){ return new Exposer(false,seckillId,nowTime.getTime(),startTime.getTime(), endTime.getTime()); } //转化特定字符串的过程,不可逆 String md5 = getMD5(seckillId);//TODO return new Exposer(true,md5,seckillId); } private String getMD5(long seckillId){ String base = seckillId+"/"+slat; String md5 = DigestUtils.md5DigestAsHex(base.getBytes()); return md5; } @Override public SeckillExecution executeSeckill(long seckillId, long userPhone, String md5) throws SeckillException, RepeatKillExeception, SeckillCloseException { if(md5==null|| !md5.equals(getMD5(seckillId))){ throw new SeckillException("seckill data rewrite"); } //执行秒杀逻辑:减库存 + 记录购买行为 Date nowTime = new Date(); try{ int updateCount = seckillDao.reduceNumber(seckillId, nowTime); if(updateCount <=0){ //没有更新记录 throw new SeckillCloseException("seckill is closed"); } else { //记录购买行为 int insertCount= successKilledDao.insertSuccessKilled(seckillId, userPhone); //唯一:insert ignore if(insertCount <=0){ //重复秒杀 throw new RepeatKillExeception("seckill repeated"); }else { //秒杀成功 SuccessKilled successKilled = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); //return new SeckillExecution(seckillId,1,"秒杀成功",successKilled); return new SeckillExecution(seckillId,SeckillStatEnum.SUCCESS); } } } catch (SeckillCloseException e1) { throw e1; } catch (RepeatKillExeception e2) { throw e2; }catch (Exception e){ logger.error(e.getMessage(),e); //所有编译期异常 转化为运行期异常 //spring事务会做roll back throw new SeckillException("seckill inner error : "+e.getMessage()); } } }
spring-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 扫描service包下所使用注解的类型 --> <context:component-scan base-package="org.seckill.service"/> </beans>
---3-1 使用Spring声明式事务理论---------------------------------------------------------
1.什么是声明式事务
通过Spring来管理(全自动),解脱事务代码
2.声明式事务使用方式:
ProxyFactoryBean + XML -------> 早期使用方式(2.0)
tx:advice + aop命名空间 ------> 一次配置永久生效
注解@Transactional (推荐) ------>注解控制()
注解控制:在控制事务方法上加入注解,在开发中大家遵守约定。
3.事务方法嵌套:
声明式事务独有的概念
传播行为--->propagation_required,如果有事务就加入到事务的原有逻辑,如果没有就创建一个新事务。
4.什么时候回滚:
抛出运行期异常(RuntimeException)
小心不当的try-catch(如果不小心抛出了编译期异常,Spring不会回滚)
---3-2 使用Spring声明式事务配置---------------------------------------------------------
---4-1 完成Service集成测试---------------------------------------------------------
Java高并发秒时啊API之Service层1