线程、异步
2015-12-01 13:49
113 查看
在业务操作时,如有用到数据同步,发送消息等业务,可用线程异步处理实现!
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service("springUtils")
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext atx;
@Override
public void setApplicationContext(ApplicationContext atx)
throws BeansException {
this.atx = atx;
}
public static ApplicationContext getAtx() {
return atx;
}
public static Object getBean(String beanName) {
return getAtx().getBean(beanName);
}
}
用构造函数传参数。
import com.alibaba.fastjson.JSON;
import com.xiexy.project.base.SpringUtils;
import com.xiexy.project.test.service.TestService;
public class SMSThread implements Runnable{
String myName;
String age;
SMSThread(String name,String age) {
myName = name;
this.age=age;
}
@Override
public void run() {
System.out.println(myName+age);
try {
System.out.println("!!!!!!!!!!");
TestService testService=(TestService) SpringUtils.getBean("testService");
System.out.println(JSON.toJSONString(testService.getStations(null)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service("springUtils")
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext atx;
@Override
public void setApplicationContext(ApplicationContext atx)
throws BeansException {
this.atx = atx;
}
public static ApplicationContext getAtx() {
return atx;
}
public static Object getBean(String beanName) {
return getAtx().getBean(beanName);
}
}
用构造函数传参数。
import com.alibaba.fastjson.JSON;
import com.xiexy.project.base.SpringUtils;
import com.xiexy.project.test.service.TestService;
public class SMSThread implements Runnable{
String myName;
String age;
SMSThread(String name,String age) {
myName = name;
this.age=age;
}
@Override
public void run() {
System.out.println(myName+age);
try {
System.out.println("!!!!!!!!!!");
TestService testService=(TestService) SpringUtils.getBean("testService");
System.out.println(JSON.toJSONString(testService.getStations(null)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 常用编码的发展史
- Mean Average Precision
- Linux NTP配置详解 (Network Time Protocol)
- 解决 “Could not find class 'com.goole.zxing.Result”和“Multiple dex files define”问题
- 性能测试-性能测试工具工作原理
- ListView的基础应用
- 栈的顺序存储实现
- Java继承中的"陷阱"问题
- Mac上Apache与PHP的配置
- 分页存储过程
- android 资源读取
- hadoop 2.6配置记录
- hadoop 2.6配置记录
- Eclipse-----切换SVN账号
- Spring使用Cache
- Android Activity的生命周期
- mysql 查看 索引
- 判断给定序列是否是对应入栈序列的出栈序列(C++)
- 设计模式之--责任链模式
- css实现移动端消息提醒按钮