spring异步处理

1、使用@Async进行注解

2、通过创建进程去处理

创建获取上下文对象实现ApplicationContextAware

/**
 * 用于获取spring上下文
 * @author WWL
 *
 */
public class ContextUtil implements ApplicationContextAware{

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext(){
        return context;
    }

    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

}

配置:

<bean id="contextUtil" class="com.ecloude.staff.thread.util.ContextUtil"/>

线程执行:

/**
 * 异步分数计算
 * @author WWL
 *
 */
public class ValueThread  implements Runnable{
    private Staff staff;
    public ValueThread(Staff entry) {
        staff=entry;
    }
    @Override
    public void run() {
        StaffService staffService=(StaffService) ContextUtil.getBean("staffService");
        staffService.getScore(staff);
    }
    
}

 

posted @ 2016-05-08 22:35  W&L  阅读(198)  评论(0编辑  收藏  举报