万能的程序员
2018-03-22 16:07:26
spring mvc中加入线程的bean解决办法
工作中遇到点ssm框架中添加线程的问题,解决办法:作为笔记记录在这。
主要看 线程类 那一步,spring中你不需要将线程类注入bean 直接 在你的线程类中,把你需要的service 和 dao 设为属性,在构造方法中传入即可。
Dao层
public interface TestDao{ @Select("select * from (select a1.*,rownum rn from (select * from table) a1 where rownum <= #{max}) where rn>= #{min}") public List<Object> getAll(@Param("min")int min,@Param("max")int max); }
Service层
@Service public class TestService { @Autowired private TestDao rd; /** * * @return 测试返回的函数 * @throws ParseException */ public List<object> getList(int min,int max) throws ParseException{ //下面的程序 放在线程中运行 List<Object> list = rd.getAll(min,max); return list1; } }
@Service public class ThreadService { @Autowired private TestService rs; @Autowired private TestDao rd; public void runt(){ //三个线程 1分钟执行 6000条 MyThread m = new MyThread(rd,rs, 0,1000); m.start(); MyThread m1 = new MyThread(rd,rs,1001,2000); m1.start(); MyThread m3 = new MyThread(rd,rs,2001,3000); m3.start(); MyThread m4 = new MyThread(rd,rs,3001,4000); m4.start(); MyThread m5 = new MyThread(rd,rs,4001,5000); m5.start(); } }
线程类
public class MyThread extends Thread{ //分页的最小值 private int min; //分页的最大值 private int max; private TestService rs; private TestDao rd; public MyThread(TestDao rd,TestService rs,int min,int max){ this.min = min; this.max = max; this.rs = rs; this.rd = rd; } public void run(){ try { rs.getList(min, max); } catch (ParseException e) { e.printStackTrace(); } } }
Controller层
@Controller public class Ridate3Controller { @Autowired private TestService rs; @Autowired private ThreadService ts; @RequestMapping("/login.htm") public String getl(HttpServletRequest request) throws ParseException{ ts.runt(); return "login"; } }
评论