单例模式

单例模式两种写法:

第一种

public class SingleSpoon  {        

   private static final SingleSpoon theSpoon=null;       

          

   private SingleSpoon() {}       

            

  public static SingleSpoon getTheSpoon() {       

      if (theSpoon == null) {       

           theSpoon = new SingleSpoon();       

       }       

       return theSpoon;       

   }       

           

   public String toString() {       

       return "Behold the glorious single spoon!";       

   }       

}     

 

第二种

public class SingleSpoon  {        

 

方案一: 就是不用单例, spring中bean的作用域设为prototype,每个请求对应一个实例.

或者取消单例模式,如配置文件修改为(Spring版本的不同,在DTD文件约束也不同):

<bean id="authgroupact" class="com.skywalk.framework.web.struts.action.AuthoriseGroupAct" autowire="byName" singleton="false">
        <property name="ibser">
            <ref bean="ibser" />
        </property>
    </bean>


方案二: spring中bean的作用域设为session ,每个session对应一个实例,解决了多线程问题.

再写一个拦截器,  清空 FieldError与actionerror

 

 

Java代码   
  1.   
  2. public class ClearFieldErrorInterceptor extends AbstractInterceptor {   
  3.   
  4. @Override  
  5. public String intercept(ActionInvocation invocation) throws Exception {   
  6. ActionSupport actionSupport = (ActionSupport)invocation.getAction();   
  7. actionSupport.clearErrorsAndMessages();   
  8. String resultCode = invocation.invoke();   
  9. return resultCode;   

参考:http://blog.sina.com.cn/s/blog_4baf091d0100gv8z.html

 

   private static final SingleSpoon theSpoon=new SingleSpoon();       

 

       // 私有的默认构造函数   

   private SingleSpoon() {}       

          // 静态工厂方法  

  public static SingleSpoon getTheSpoon() {       

           

       return theSpoon;       

   }       

           

   public String toString() {       

       return "Behold the glorious single spoon!";       

   }       

}     

 

由于单例模式不能被继承,可以使用一种特殊化的单例模式,单例注册表

 

spring 默认使用单例模式进行创建,也可以设置成原型模式,spring框架对单例的支持是使用单例注册表的方式进行的

 

具体参考:http://www.360doc.com/content/09/0725/10/61497_4436510.shtml

 

http://tailsherry.iteye.com/blog/124687

 

http://blog.youkuaiyun.com/liyong1115/archive/2008/01/22/2059170.aspx

 

strut2跟spring整合后,由于strut2中每次有一个请求就创建一个action实例,而spring是采用的单例模式,所以用spring管理struts2的action时就会出现下面的问题:

1) Action是单例,其中的FieldError,actionerror中的错误信息 会累加, 即使再次输入了正确的信息,也过不了验证.

2) Struts2的Action是有状态的,他有自己的成员属性, 所以在多线程下,会有问题.

参考:http://elf8848.iteye.com/blog/356746

期末大作业基于python的足球运动员数据分析源码+数据集(高分项目),个人经导师指导并认可通过的高分设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在做大作业、毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于pyth
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值