spring 属性注入单例类失败?何解?代码如下
<!-- 数据层依赖注入 -->
<bean id="msgDao" class="com.earth.dao.impl.MsgDaoImpl" >
<!-- 注入持久化操作所需的SessionFactory -->
<property name="hibernater">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<!-- 业务处理层 依赖注入-->
<bean id="chatManager" class="com.earth.services.ChatManager">
<property name="msgDao">
<ref bean="msgDao" />
</prop
属性msgDao注入不了一直是null!!!!
package com.earth.services;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import com.earth.dao.MsgDao;
import com.earth.dao.impl.MsgDaoImpl;
import com.earth.entity.Msg;
import com.earth.infrastructure.BaseData.MsgType;
/**
* 单例会话管理类,使用静态内部类实现线程安全
* @author Administrator
*
*/
public class ChatManager {
private static class LazyHolder {
private static final ChatManager INSTANCE = new ChatManager();
}
private ChatManager (){}
public static final ChatManager getInstance() {
return LazyHolder.INSTANCE;
}
/**
* 内存中保存的消息最大长度
*/
public int MsgStoreLength;
/**
* 是否正在处理匿名消息
*/
public boolean isHandleAnonymous;
/**
* 会话消息列表
*/
public Hashtable<String,List<Msg>> sessionMsgList;
/**
* 房间人数统计列表
*/
public Hashtable<String,Integer> RoomCount;
/**
* 匿名聊天等待队列
*/
public Hashtable<String,Date> AnonymousWait;
/**
* 匿名聊天等待队列
*/
public Hashtable<String,Date> AnonymousChat;
/**
* 匿名聊天等待队列
*/
public Hashtable<String,Date> AnonymousDisconnect;
/**
* 数据处理层对象
*/
private MsgDao msgDao;
public MsgDao getMsgDao() {
return msgDao;
}
public void setMsgDao(MsgDao msgDao) {
this.msgDao = msgDao;
}
/**
* 用户上线消息处理
*/
public void handleOnline(Msg para) {
String key = para.getRecipient();
if (this.RoomCount == null) {
// 添加人数
this.RoomCount = new Hashtable<String, Integer>();
this.RoomCount.put(key, 1);
} else if (!this.RoomCount.containsKey(key)) {
this.RoomCount.put(key, 1);
} else if (this.RoomCount.containsKey(key)) {
this.RoomCount.put(key, this.RoomCount.get(key) + 1);
}
saveSessionMsg(para);
}
/**
* 用户一般消息处理
*/
public void handleMsg(Msg para) {
saveSessionMsg(para);
}
/**
* 用户离线消息处理
* @param para
*/
public void handleOffline(Msg para) {
String key = para.getRecipient();
//匿名聊天则删除整个小组
if(key.contains("/queue/AnonymousPair"))
{
//处于等待状态,直接从等待队列移除
if(key.equals("/queue/AnonymousPair"))
{
this.AnonymousWait.remove(para.getCoordinate());
para.setRecipient("");
}
else
{
//聊天状态断开
//1、删除小组
this.RoomCount.remove(key);
//2、队列数据处理
if(this.AnonymousChat.containsKey(para.getCoordinate()))
this.AnonymousChat.remove(para.getCoordinate());
String temp = key.replace("/queue/AnonymousPair", "").replace(para.getCoordinate(), "");
if(this.AnonymousChat.containsKey(temp))
this.AnonymousChat.remove(temp);
}
}
else
this.RoomCount.put(key, this.RoomCount.get(key) - 1);
saveSessionMsg(para);
List<Msg> msglist = this.sessionMsgList.get(key);
// if(this.msgDao==null)this.msgDao = new MsgDaoImpl();
this.getMsgDao().saveMsg(msglist);
if(!key.contains("/queue/AnonymousPair")&&this.RoomCount.get(key) ==0)
{
msglist = this.sessionMsgList.get(key);
//将消息队列存入数据库
// this.msgDao.deleteMsg(para.getRecipient());
this.msgDao.saveMsg(msglist);
this.sessionMsgList.remove(key);
}
}
/**
* 匿名配对请求处理
* @param para
*/
public Msg handleAnonymousPair(Msg para){
Iterator<String> iter;
String key = null;
Msg result = null;
if(this.AnonymousWait!=null&&this.AnonymousWait.size()>0)
{
iter = this.AnonymousWait.keySet().iterator();
if (iter.hasNext()) {
//拿取等待最久的用户,建立连接
key = (String)iter.next();
}
}
else if(this.AnonymousWait==null)
{
this.AnonymousWait = new Hashtable<String,Date>();
this.AnonymousWait.put(para.getCoordinate(), new Date());
}else if(this.AnonymousWait.size()<=0)
{
this.AnonymousWait.put(para.getCoordinate(), new Date());
}
result = new Msg();
result.setMsgType(MsgType.AnonymousPair);
if(key!=null&&key!=para.getCoordinate())
{
//设置进入会话组
result.setRecipient("/queue/AnonymousPair"+key+para.getCoordinate());
handleOnline(result);
handleOnline(result);
//拼接订阅地址
result.setCoordinate(key);
result.setRecipient(para.getCoordinate());
result.setMsg(key+para.getCoordinate());
//换队列
this.AnonymousWait.remove(key);
if(this.AnonymousChat==null)this.AnonymousChat = new Hashtable<String,Date>();
this.AnonymousChat.put(key, new Date());
this.AnonymousChat.put(para.getCoordinate(), new Date());
}
else
{
result.setMsg("nopair");
result.setCoordinate(para.getCoordinate());
}
return result;
}
/**
* 保存会话消息
* @param para
*/
private void saveSessionMsg(Msg para)
{
String key = para.getRecipient();
//匿名对聊不保存历史
if(key.contains("/queue/AnonymousPair"))return;
List<Msg> msglist =null;
if(this.sessionMsgList==null)
{
this.sessionMsgList = new Hashtable<String, List<Msg>>();
msglist = new ArrayList<Msg>();
}
else if(!this.sessionMsgList.containsKey(key))
{
msglist = new ArrayList<Msg>();
}
else if(this.sessionMsgList.containsKey(key))
{
msglist = this.sessionMsgList.get(key);
}
if (msglist.size() >= this.MsgStoreLength)
msglist.remove(0);
msglist.add(para);
this.sessionMsgList.put(key, msglist);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
能否给出详细的控制台输出?怎么公共类可以放在同一个源文件里面