现在主流的java的前端框架有:struts1,struts2,springmvc 还有最根本的servlet;
前些天一个朋友问我这方面的问题,就研究一番:
1.关于struts1:
Struts1使用的ActionServlet是单例的,由这一个servlet处理所有.do请求。RequestProcessor也是单例。
RequestProcessor的processActionCreate方法:
/**
* <p>Return an <code>Action</code> instance that will be used to process
* the current request, creating a new one if necessary.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param mapping The mapping we are using
* @return An <code>Action</code> instance that will be used to process
* the current request.
* @throws IOException if an input/output error occurs
*/
protected Action processActionCreate(HttpServletRequest request,
HttpServletResponse response, ActionMapping mapping)
throws IOException {
// Acquire the Action instance we will be using (if there is one)
String className = mapping.getType();
if (log.isDebugEnabled()) {
log.debug(" Looking for Action instance for class " + className);
}
Action instance;
// 这个同步快保证了Action的单例
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
}
// Create and return a new Action instance
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
}
try {
instance = (Action) RequestUtils.applicationInstance(className);
// Maybe we should propagate this exception
// instead of returning null.
} catch (Exception e) {
log.error(getInternal().getMessage("actionCreate",
mapping.getPath()), e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("actionCreate", mapping.getPath()));
return (null);
}
actions.put(className, instance);
if (instance.getServlet() == null) {
instance.setServlet(this.servlet);
}
}
return (instance);
}
从结果可以知道,是单例,既然是单例,如果当使用实例变量的时候就会有线程安全的问题;
2.关于struts2
我们知道我们使用struts2的时候,都是使用actionContext ;都是使用里面的实例变量,让struts2自动匹配成对象的。如果不是线程安全那全完了;所以struts2必须是线程安全的;因为每次处理一个请求,struts就会实例化一个对象;这样就不会有线程安全的问题了;
哦,忘记了一种情况,struts2+spring来管理注入的时候;如果把action设置为单例模式,就会出现问题;可以把action设置为prototype类型,还有一个办法是设置作用域(具体没有实验过)
3.关于SpringMVC
SpringMVC的controller默认是单例模式的,所以也会有多线程并发的问题;
参考代码:
@RequestMapping("/user")
@Controller
Class UserController
{
@Resource
UserService userService;
@RequestMapping("/add")
public void testA(User user){
userService.add(user);
}
@RequestMapping("/get")
public void testA(int id){
userService.get(id);
}
}
@Service("userService")
Class UserService{
public static Map<Integer,User> usersCache = new HashMap<String,User>();
public void add(User user){
usersCache.put(user.getId(),user);
}
public void get(int id){
usersCache.get(id);
}
}
usersCache就是非线程安全的。
解决方法:
1)同步共享数据
2)不使用成员实例变量;
3)使用只读数据
相关推荐
java开发面试基础,第一部分包含虚拟机、基础语法、面向对象、异常处理、IO、集合、线程、网络、高级问题。第二部分包括HTML、JS、JQUERY、XML。第三部分包括服务器、JSP、JDBC、servlet、filter、listener。第四...
java进阶源码分析专题常用设计模式线程与并发锁的使用深度理解synchronized、volatile、cas手写ASQSpring5IOC容器设计原理及高级特性AOP设计原理FactoryBean与BeanFactorySpring事务处理机制Spring JDK动态代理...
即如何判断是否有线程安全问题 什么是markword 乐观锁和悲观锁 偏向锁 轻量级锁 自旋锁 自适应自旋锁 重量级锁 synchronized 可重入锁 土方法实现可重入锁 使用AQS类实现可重入锁 CAS MySQL 中的行级锁、表级锁和页...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
Spring整合Hibernate、Spring整合Struts,SpringMVC技术。 深入理解Ioc和AOP的基本原理和实现方式;熟练掌握SpringIoc及AOP实现方式;熟练掌握Spring事务管理;熟练掌握Spring与其他组件的整合技术。• 实训项目五:...
熟练使用spring,struts2,hibernate,springmvc,mybatis,springboot等架构的以及各相互间的整合开发(ssh,ssm),配置及对象的设计和开发。 对redis缓存,ActionMQ消息队列有一定的了解使用。 熟练使用jsp、servlet、...
Java面试题51.SpringMVC和Struts2的不同 Java面试题52.简单介绍一下Spring或者Spring的两大核心 Java面试题53.AOP是什么?都用它做什么? Java面试题54.Spring事务的传播特性和隔离级别 Java面试题55.ORM是什么?ORM...
面试题包含了不同技术层面的面试问题,同时也能对一些没有面试开发经验的小白给予不可估量的包装, 让你的薪水绝对翻倍, 本人亲试有效.Java面试题84集、java面试专属及面试必问课程,所有的面试题有视屏讲解, 解答方案....
掌握主流的Java开源框架Struts2、Spring、SpringMVC、Hibernate/Mybatis、iBatis等,最好熟悉Freemaker或volecity。 2. 熟悉中间件Tomcat、jboss、Apache、Weblogic、WAS; 3. 熟悉各种Web前端技术,包括JavaScript...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
java高级部分(集合、多线程、正则表达式等)--->html5+css3--->javascript--->jquery--->mysql数据库--->jsp/servlet--->oracle数据库--->项目讲解--->Mybatis框架---->spring框架---->...
:memo:Java多线程 :skis:JVM/NIO/...等等 :guitar:设计模式 :dollar_banknote:Java面试题 :page_facing_up:JavaWeb技术 :ledger:Servlet+Tomcat :microscope:JSP :ring:JDBC :t-shirt:XML和JSON :ribbon:过滤器和...