springboot结合Feture,解决@Async无效,不起作用

Feture简介
在这里插入图片描述
常用的方法介绍
在这里插入图片描述
易出错的地方
在这里插入图片描述

@Service
public class XxxService{
  public void methodA(){
    ...
    XxxService xxxServiceProxy = SpringUtil.getBean(XxxService.class);
    xxxServiceProxy.methodB();
    ...
  }
 
  @Async
  public void methodB() {
    ...
  }
}

package com.zhiliyuchi.web.rest.controller;
 
import com.zhiliyuchi.domain.fishpond.ManagerEntity;
import com.zhiliyuchi.repository.fishpond.ManagerRepository;
import com.zhiliyuchi.web.rest.util.Result;
import com.zhiliyuchi.web.rest.util.SpringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
/**
 * @创建人:Young
 * @时 间: 2019/3/13
 * @描 述: Manager类Controller接口类
 */
@RestController
@RequestMapping("manager")
public class ManagerController {
    private final Logger logger = LoggerFactory.getLogger(ManagerController.class);
    @Autowired
    private ManagerRepository managerRepository;
//    @Autowired
//    private AsyncController asyncController;
 
    /**
     * @描述 查询所有的数据的异常测试类
     * @参数  []
     * @返回值  com.zhiliyuchi.web.rest.util.Result
     * @创建人  Young
     * @创建时间  2019/3/13 14:05
     **/
    @GetMapping("all")
    public Result getAll(){
        ManagerController bean = SpringUtils.getBean(ManagerController.class);
        logger.debug("查询所有的manager");
        List<ManagerEntity> all = managerRepository.findAll();
        bean.test();
        bean.test();
        return Result.error("数据请求服务未启用","type","subtype");
    }
 
 
    @Async
    public void test(){
        try {
            logger.debug("=======开始执行异步处理类========");
            Thread.sleep(2000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
 
 
 
}

StringUtils的工具类,手动获取bean方法:

package com.zhiliyuchi.web.rest.util;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
/**
 * @创建人:Young
 * @时 间: 2019/3/13
 * @描 述: 手动获取spring的bean
 */
@Component("springContextUtil")
public class SpringUtils implements ApplicationContextAware {
 
 
    private static ApplicationContext applicationContext = null;
 
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanId) {
        return (T) applicationContext.getBean(beanId);
    }
 
    public static <T> T getBean(Class<T> requiredType) {
        return (T) applicationContext.getBean(requiredType);
    }
    /**
     * Spring容器启动后,会把 applicationContext 给自动注入进来,然后我们把 applicationContext
     *  赋值到静态变量中,方便后续拿到容器对象
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值