📄 详细分析一.txt
字号:
logger.info("Init Cache...");
properties.load(classPathResource.getInputStream());//使用Properties的load(InputStream inStream) 来读取配置文件的时候
admin = new GeneralCacheAdministrator(properties);
} catch (Exception ex) {
logger.error(ex);
admin = new GeneralCacheAdministrator();
}
}
注意,这个admin对象来自OSCache包,用于管理Cache内容吧.另外,我们看看其对Cache的实现:
public void add(Object key, Object value) {
logger.debug("Add into cache [Key:" + key + "]");
this.admin.putInCache(String.valueOf(key), value);
}
public Object get(Object key) {
try {
logger.debug("Get from cache [Key:" + key + "]");
return this.admin.getFromCache(String.valueOf(key));
} catch (NeedsRefreshException ex) {
logger.debug("Object not in cache, return null");
this.admin.cancelUpdate(String.valueOf(key));
return null;
}
}
public void remove(Object key) {
logger.debug("Remove from cache [Key:" + key + "]");
this.admin.flushEntry(key.toString());
}
public void removeAll() {
logger.debug("Remove all");
this.admin.flushAll();
}
这样就可以对缓存内容进行控制操作了(就用这几个方法)!让我们回到BoardPermissionSerivceImp类中:
注入了DAO和Cache(userPermissionCache)后,进行方法实现时,主要还是DAO去做,不过在saveBoardPermission和updateBoardPermission,还有各种remove方法中还用了this.clearPermissionCache();(除了find),让我们来看看它的代码吧:
private void clearPermissionCache() {
if (Constant.USE_PERMISSION_CACHE) {
//public static final boolean USE_PERMISSION_CACHE = true;
this.getUserPermissionCache().removeAll();//调用Cache中的方法哦!removeALL看上面this.admin.flushAll();!
}
}
555555,这个服务类也了Cache服务(与别的服务结合了),下面是DAO实现:
private static final String LOAD_BY_BID_GID ="from BoardPermission where boardID = ? and groupID = ?";
private static final String LOADS_BY_BID = "from BoardPermission where boardID = ?";
private static final String LOADS_BY_GID = "from BoardPermission where groupID = ?";
private static final String REMOVE_BY_BID = "delete from BoardPermission where boardID = ?";
private static final String REMOVE_BY_GID = "delete from BoardPermission where groupID = ?";
我们看update:
public BoardPermission updateBoardPermission(BoardPermission bp) {
//System.out.println("update bp");
this.getHibernateTemplate().update(bp);
return bp;
}
再看:
/**
* 根据GroupID删除BoardPermission对象
*
* @param gid int
* @todo Implement this com.laoer.bbscs.dao.BoardPermissionDAO method
*/
public void removeBoardPermissionsByGid(final int gid) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException, SQLException {
Query query = s.createQuery(REMOVE_BY_GID);
query.setLong(0, gid);
query.executeUpdate();
return null;
}
});
}
接下来,是BoardSaveService:
先看bean吧,它实现了implements Serializable...,加入private static final long serialVersionUID = 5390014211916049604L;它有三个属性:
private String id;
private String userID;
private long boardID;
[
Java的JavaBeans. Bean的状态信息通常是在设计时配置的。Bean的状态信息必须被存起来,以便当程序运行时能恢复这些状态信息。这也需要serializaiton机制。
总之如果在网络的环境下做类传输,应该还是implements Serializable。
]
再看其hbm.xml:
<hibernate-mapping package="com.laoer.bbscs.bean">
<class name="BoardSave" table="bbscs_boardsave">
<id name="id" column="ID" type="string" unsaved-value="null">
<generator class="uuid"/>
</id>
<property column="UserID" length="40" name="userID" not-null="true" type="string"/>
<property column="BoardID" length="13" name="boardID" not-null="true" type="long"/>
</class>
</hibernate-mapping>
从这样便知是用于收藏版区的对象模型啊!那接下来看其接口中的方法:
public BoardSave saveBoardSave(BoardSave boardSave) throws BbscsException;
public BoardSave findBoardSaveById(String id);
public BoardSave findBoardSaveByUidBid(String userId, long bid);
public List findBoardSavesByUid(String userId);
public List findBoardSaveBidsByUid(String userId);//找Bid的List
public void removeBoardSave(BoardSave boardSave) throws BbscsException;
public void removeBoardSaveByUidBid(String userId, long bid) throws BbscsException;
public void removeBoardSaveByBid(long bid) throws BbscsException;
public void removeBoardSaveByBidsUid(String userId, List ids) throws BbscsException;
对于imp调用dao-->daoimp我们直接进入dao的imp中:
其中的常量定义如下:
private static final String LOAD_BY_UID_BID = "from BoardSave where userID = ? and boardID = ?";
private static final String LOADS_BY_USERID = "from BoardSave where userID = ?";
private static final String LOADS_BOARDID_BY_USERID = "select boardID from BoardSave where userID = ?";//很特别哦!(其实也没什么,相对而已)
private static final String REMOVE_BY_UID_BID = "delete from BoardSave where userID = ? and boardID = ?";
private static final String REMOVE_BY_BID = "delete from BoardSave where boardID = ?";
private static final String REMOVE_IN_IDS_BY_UID =
"delete from BoardSave where userID = :userID and boardID in (:ids)";
我们来看方法实现吧.
public BoardSave saveBoardSave(BoardSave boardSave) {
this.getHibernateTemplate().saveOrUpdate(boardSave);
return boardSave;
}
可用于保存,也可以用于更新!
public List findBoardSaveBidsByUid(String userId) {
return this.getHibernateTemplate().find(LOADS_BOARDID_BY_USERID, userId);
//一个参数
public void removeBoardSaveByBidsUid(final String userId, final List ids) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException, SQLException {
Query query = s.createQuery(REMOVE_IN_IDS_BY_UID);
query.setString("userID", userId);
query.setParameterList("ids", ids);
query.executeUpdate();
return null;
}
});
}
OK!
接下来,是BoardService,先看其相关的Bean!它也实现了可序列化操作...它的属性特别多..
很关键哦!
private Long id;//主键
private long parentID;//父级版区ID
private List parentIDs;//所有父级版区ID列表
private List childIDs;
private String boardName;//版区名称
private String explains;(说明)
private String bulletin;(公告)
private String boardPic;//图片
private int useStat;(使用状态)
private int orders;//序
private int needPasswd;//是否需要密码访问
private String passwd;//访问密码
private int level;---->数据库中的BoardLevel//级别
private int boardType;(类型1,2,3)//版区类型
private int allowHTML;//HTML是否支持
private int allowUBB;//UBB...
private int auditPost; (帖子是否需要审核)
private int auditAttach;//附件是否需要审核
private int addUserPostNum;//是否增加用户发贴数
private int isHidden;
private int isAuth;//是否需要论证用户才能访问
private long mainPostNum;//主贴数
private long postNum;//帖子数量
private Map boardMaster=new HashMap();
private Set boardTag=new HashSet();
我们看看Board.hbm.xml,错了,我们应该看看applicationCotext.xml中的内容是不是这个,经查看:
<value>com/laoer/bbscs/bean/Board-${datasource.type}.hbm.xml</value>,我用mysql当然是Board-mysql.hbm.xml,其内容主要如下:
<hibernate-mapping package="com.laoer.bbscs.bean">
<class name="Board" table="bbscs_board">
<id name="id" column="ID" type="long" unsaved-value="null">
<generator class="identity"/> //long类型的identity!
</id>
<property column="ParentID" length="20" name="parentID" not-null="true" type="long"/>
<property column="ParentIDs" name="parentIDs" type="com.laoer.bbscs.ext.hibernate.SplitList"/>//parentIDs有用了userType
<property column="ChildIDs" name="childIDs" type="com.laoer.bbscs.ext.hibernate.SplitList"/>
<property column="BoardName" length="60" name="boardName" not-null="true" type="string"/>
<property column="Explains" name="explains" type="text"/>//text类型
<property column="Bulletin" name="bulletin" type="text"/>
<property column="BoardPic" length="200" name="boardPic" type="string"/>
<property column="UseStat" length="1" name="useStat" type="int"/>//一个开关的功能
<property column="Orders" length="11" name="orders" type="int"/>//论坛排序
<property column="NeedPasswd" length="1" name="needPasswd" type="int"/>//开关
<property column="Passwd" length="100" name="passwd" type="string"/>
<property column="BoardLevel" length="11" name="level" type="int"/>
<property column="BoardType" length="2" name="boardType" type="int"/>
<property column="AllowHTML" length="1" name="allowHTML" type="int"/>
<property column="AllowUBB" length="1" name="allowUBB" type="int"/>
<property column="AuditPost" length="1" name="auditPost" type="int"/>
<property column="AuditAttach" length="1" name="auditAttach" type="int"/>//不懂???
<property column="AddUserPostNum" length="1" name="addUserPostNum" type="int"/>//应该是一个开关吧
<property column="IsHidden" length="1" name="isHidden" type="int"/>
<property column="IsAuth" length="1" name="isAuth" type="int"/>
<property column="MainPostNum" length="11" name="mainPostNum" type="long"/>
<property column="PostNum" length="11" name="postNum" type="long"/>
//关键点,boardMaster和boardTag均为one-to-many类型!
<map name="boardMaster" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="boardID"/>
<map-key column="UserName" type="string"/>
<one-to-many class="BoardMaster"/>
</map>
<set name="boardTag" inverse="true" cascade="all-delete-orphan" lazy="false" order-by="orders asc">
<key column="boardID"/>
<one-to-many class="BoardTag"/>
</set>
</class>
</hibernate-mapping>
看完这些后,我们就知道这个bean是用于版区操作了,让我们看看方法:(先看接口BoardService)
public Board saveBoard(Board board) throws BbscsException;//保存或更新Board对象
public Board createBoard(Board board) throws BbscsException;
public Board updateBoard(Board board, long oldParentID) throws BbscsException;
public Board getBoardByID(long id);
public List findBoardsByParentID(long pid, int useStat, int hidden, int orderType);
public List findBoardsAllTree(long pid, List topList, int useStat, int hidden, int orderType);
public int getNextOrder(long pid);
public int getPostSumNum(int mainorall, int useStat, int hidden);
public void removeBoard(Board board) throws BbscsException;
public Map[] getBoardPermission(long bid, int groupID);
public Map[] getBoardMasterPermission(int roleID);
public boolean isBoardMaster(Board board, String userName);
public List findBoardsInIDs(List ids);
public void removeBoardTag(Board board, String tagID) throws BbscsException;
public List getBoardIDs(List boards);
public void saveBoardsPostNumCount() throws BbscsException;
这里方法有16个左右,注意我们进行除了查询后的其它CURD操作都可能会产生异常.对于具体方法我们需要去了解其实现才能理清其作用.看下服务实现层:(注BoardServiceCacheImp为其实现类,可能是加入了许多缓存的原因吧):
首先仍是logger(这里发现了原来其实在service层和dao层只有这里需要logger,其它地方都没,对于异常也只有service接口和实现层有BbscsException这个异常处理,而对于DAO层的异常则已经由HibernateTemplate完全抛出了,如:
public void saveOrUpdate(final Object entity)
throws DataAccessException
{
execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException
{
checkWriteOperationAllowed(session);
session.saveOrUpdate(entity);
return null;
}
}
, true);
}
)
我们回到正题上,除了logger,还有boardDAO(当然要了)
,userGroupDAO,boardPermissionDAO(讲过的),permissionDAO,roleDAO,forumDAO,forumHistoryDAO,还有一些Cache服务:sysListObjCache,boardCache,userPermissionCache(以前用过),还有一个额外的服务类:sysStatService;对他们进行getter/setter方法一下...
我们分析下sysStatService:(applicationContext.xml) 系统统计服务
<bean id="sysStatService"
class="com.laoer.bbscs.service.imp.SysStatServiceImp"
scope="prototype"> //每次请求都产生一个对象
<property name="userConfig">
<ref bean="userConfig" />
</property>
</bean>
userConfig指:
<bean id="userConfig"
class="com.laoer.bbscs.service.config.UserConfig">
<property name="safePath">
<value>${bbscs.safePath}</value> //安全目录我的指的是D:/safe/
</property>
</bean>
UserConfig是一个config配置服务,在服务类中首先注入了safePath 这个String对象,它向外提供了String getUserFilePath(String userID)[简单,最终得到一个用户文件Path,见实际目录就可知道其原理]和String getIndexPath()和File getIndexFilePath()以及boolean indexExist()四个对外公共方法.详细看方法实现:
public String getIndexPath() { //好象没用到过
StringBuffer sb = new StringBuffer();
sb.append(this.getSafePath());
sb.append("index/");
return sb.toString();
}
public File getIndexFilePath() {
File indexFilePath = new File(this.getIndexPath());//构造一个File对象
if (!indexFilePath.exists()) {
indexFilePath.mkdirs();
}
return indexFilePath;
}
public boolean indexExist() {
File file = new File(this.getIndexPath() + "segments");
return file.exists();//使用文件是否存在的方法作判断!
}
可见config服务类较简单(没用到logger)!提供一个常用服务!而com.laoer.bbscs.service.imp.SysStatService是怎么用到它的呢?它继承了抽象类SysStatService,在这个抽象类中,有一个东西是私有的:
private long onlineNum = 0;
private long appearTime = 0; 发表时间long类型
private String appearTimeStr = "";
private long allUserNum = 0;
private String lastRegUser = "";
private long postMainNum = 0;
private long postNum = 0;
(这个东西和safe文件夹下的sysstat.properties好像啊,看看先:
#sysstat.properties
#Mon Jul 16 11:35:16 CST 2007 //appearTimeStr ??
onlineNum=2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -