cache注解实效
@Service
public class CacheService {
@Cacheable(value = "listCache", key = "'_selectData_'+#abc")
public List<String> selectData(String abc) {
List<String> list = Lists.newArrayList("1","2","3");
//从线程中获取代理对象
CacheService cacheService = SpringBeanUtil.getBean(CacheService.class);
List<String> list2 = cacheService.findData("1");
//cache注解实效
//List<String> list2 = this.findData();
list.addAll(list2);
return list;
}
@Cacheable(value = "listCache", key = "'_findData_'+#myId")
public List<String> findData(String myId) {
//数据库查询操作........
return Lists.newArrayList("3","4","5");
}
}
/**
* @author guokaige
*
*/
@Component("springBeanUtilCore")
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringBeanUtil.applicationContext == null) {
SpringBeanUtil.applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过Bean名字获取Bean
*
* @param beanName
* @return
*/
public static Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
/**
* 通过Bean类型获取Bean
*
* @param beanClass
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> beanClass) {
return getApplicationContext().getBean(beanClass);
}
/**
* 通过Bean名字和Bean类型获取Bean
*
* @param beanName
* @param beanClass
* @param <T>
* @return
*/
public static <T> T getBean(String beanName, Class<T> beanClass) {
return getApplicationContext().getBean(beanName, beanClass);
}
}
事务注解实效
@Service
public class TranService {
@Transactional(rollbackFor = Exception.class)
public void add() {
//数据库操作........
//从线程中获取代理对象
TranService tranService = (TranService) AopContext.currentProxy();
tranService.cut();
//事务注解实效
//this.cut();
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void cut() {
//数据库操作........
}
}