《Spring》Spring注解失效---通过this调用方法, 被调方法的注解失效问题: 事务注解失效,缓存注解失效

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() {
		
		//数据库操作........
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值