package com.github.wuhulala.service;
import com.github.wuhulala.mappers.SecKillDao;
import com.github.wuhulala.model.SecKill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author xueaohui
*/
@Service
public class SecKillService {
@Autowired
private SecKillDao secKillDao;
//@CachePut(value = "seckills",key = "#secKill.id")
public int insertSecKill(SecKill secKill) {
return secKillDao.createSecKill(secKill);
}
@Cacheable(value = "seckills", key = "#secKill.id")
public SecKill findOne(SecKill secKill) {
System.out.println("缓存中没有 开始查询");
return secKillDao.getSecKill(secKill.getId());
}
@CachePut(value = "seckills", key = "#secKill.id")
public SecKill update(SecKill secKill) {
System.out.println("更新secKill-----" + secKill);
secKillDao.updateSecKill(secKill);
return secKill;
}
@Transactional
public int getSecKill(int id) {
SecKill secKill = findOne(new SecKill(id));
int number = secKill.getNumber();
if (number > 0) {
secKill.setNumber(number - 1);
update(secKill);
return id;
} else {
return -1;
}
}
}
import com.github.wuhulala.AppConfig;
import com.github.wuhulala.model.SecKill;
import com.github.wuhulala.service.SecKillService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertEquals;
/**
* @author xueaohui
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
@WebAppConfiguration("src/main/resources")
public class SecKillTest {
@Autowired
private SecKillService service;
@Test
public void TestGetSecKill(){
for(int i = 0 ; i < 10 ; i++) {
System.out.println(service.getSecKill(9));
}
}
}
/*
缓存中没有 开始查询
23:51:35,977 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,042 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,072 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
更新secKill-----SecKill{id=9, name='秒杀产品1', number=0}
23:51:36,075 DEBUG com.github.wuhulala.mappers.SecKillDao.updateSecKill (139) - ==> Preparing: update seckills set number = ? , name = ? where id=?
23:51:36,078 DEBUG com.github.wuhulala.mappers.SecKillDao.updateSecKill (139) - ==> Parameters: 0(Integer), 秒杀产品1(String), 9(Integer)
23:51:36,081 DEBUG com.github.wuhulala.mappers.SecKillDao.updateSecKill (139) - <== Updates: 1
9
缓存中没有 开始查询
23:51:36,090 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,092 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,095 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,099 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,101 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,103 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,108 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,109 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,112 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,119 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,121 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,124 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,128 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,129 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,135 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,140 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,142 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,145 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,149 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,150 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,155 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,162 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,163 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,166 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
缓存中没有 开始查询
23:51:36,171 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Preparing: select * from seckills where id=?
23:51:36,173 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - ==> Parameters: 9(Integer)
23:51:36,176 DEBUG com.github.wuhulala.mappers.SecKillDao.getSecKill (139) - <== Total: 1
-1
*/
L'utilisation du proxy automatique aop (spring default) ne peut pas résoudre le problème des appels internes dans la classe. Si vous souhaitez résoudre ce problème, vous devez utiliser aspectj (spring peut être intégré à aspectj). Aspectj utilise le tissage à l'exécution ou le tissage à la compilation, ce qui modifiera le bytecode, ce qui peut résoudre ce problème.
Ce n'est vraiment pas possible. Cela est dû au principe du spring aop
Spring
Appel au sein de la classethis
pour obtenir l'objet cible, pas l'objet proxyPar
BeanPostProcessor
, une fois la classe initialisée normalement, injectez-vous dans le beanRécupérez l'objet proxy actuel via
AopContext.currentProxy