在布置memcached的缓存的问题时候,遇到问题如下:
1.布置memcached的xml配置,memcached.xml
<?xml version="1.0" encoding="UTF-8"?>
<memcached>
<!-- name 属性是程序中使用Cache的唯一标识;socketpool 属性将会关联到后面的socketpool配置; -->
<client name="mclient_0" compressEnable="true" defaultEncoding="UTF-8"
socketpool="pool_0">
<!-- 可选,用来处理出错情况 -->
<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler>
</client>
<!--
name 属性和client 配置中的socketpool 属性相关联。
maintSleep属性是后台线程管理SocketIO池的检查间隔时间,如果设置为0,则表明不需要后台线程维护SocketIO线程池,默认需要管理。
socketTO 属性是Socket操作超时配置,单位ms。 aliveCheck
属性表示在使用Socket以前是否先检查Socket状态。
-->
<socketpool name="pool_0" maintSleep="5000" socketTO="3000"
failover="true" aliveCheck="true" initConn="5" minConn="5" maxConn="250"
nagle="false">
<!-- 设置memcache服务端实例地址.多个地址用","隔开 -->
<servers>127.0.0.1:11211</servers>
<!--
可选配置。表明了上面设置的服务器实例的Load权重. 例如 <weights>3,7</weights> 表示30% load 在
10.2.224.36:33001, 70% load 在 10.2.224.46:33001
<weights>3,7</weights>
-->
</socketpool>
</memcached>
2.memcached的tool工具
package com.lqg.memcache;
import com.alisoft.xplatform.asf.cache.ICacheManager;
import com.alisoft.xplatform.asf.cache.IMemcachedCache;
import com.alisoft.xplatform.asf.cache.memcached.CacheUtil;
import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;
public class MemCachedTool {
private ICacheManager <IMemcachedCache> manager;
private IMemcachedCache cache;
public MemCachedTool(){
manager=CacheUtil.getCacheManager(IMemcachedCache.class,MemcachedCacheManager.class.getName());
manager.setConfigFile("memcached.xml");
manager.setResponseStatInterval(5*1000);
manager.start();
cache=manager.getCache("mclient_0");
}
/**
* 获取缓存接口
* @return
*/
public IMemcachedCache getCache(){
return this.cache;
}
/**
* 数据放入缓存
* @param key
* @param object
*/
public void put(String key, Object object){
cache.put(key, object);
}
/**
* 从缓存中读取数据
* @param key
* @return
*/
public Object get(String key){
return cache.get(key);
}
}
3.memcached的tool工具的spring配置
<!-- MemcachedCache缓存 -->
<bean id="MemcachedCache" class="com.lqg.memcache.MemCachedTool"></bean>
4.aop配置类
package com.lqg.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lqg.memcache.MemCachedTool;
import com.lqg.model.user.Admin;
@Aspect
public class AdminInterceptor {
public static final Logger log=Logger.getLogger(AdminInterceptor.class);
//将缓存客户端工具类MemcahcedTool编织进来
@Autowired
private MemCachedTool memcachedCache;
/*
* 定义poincut
*/
@Pointcut("execution(* com.lqg.dao.user.AdminDaoImpl.*(..))")
public void aPointcut(){
}
/**
* 环绕装备 用于拦截查询 如果缓存中有数据,直接从缓存中读取;否则从数据库读取并将结果放入缓存
*
* @param call
* @param name
* @return
*/
@Around("aPointcut()&&args(username,password)")
public Admin doFindUserByNameAround(ProceedingJoinPoint call, String username,String password) {
Admin admin = null;
if (memcachedCache.getCache().containsKey("findAdminByName_" + username)) {
admin = (Admin) memcachedCache.get("findAdminByName_" + username);
log.debug("从缓存中读取!findAdminByName_" + username);
System.out.println("从缓存中读取!findAdminByName_" + username);
} else {
try {
admin = (Admin) call.proceed();
if (admin != null) {
memcachedCache.put("findAdminByName_" + username, admin);
log.debug("缓存装备被执行:findAdminByName_" + username);
System.out.println("缓存装备被执行:findAdminByName_" + username);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
return admin;
}
}
其中会出现问题;
3.1.本人JDK是1.7的,于是尝试使用不同版本的aspectjrt.jar文件,发现错误仍然存在!所以不是aspectjrt.jar的问题。
于是本人下载了最新版本的aspectjweaver.jar并替换了原来的版本,error at ::0 can‘t find referenced pointcut allAddMethod成功解决了,程序正常运行。
3.2.error at ::0
formal unbound in pointcut
解决方法:去掉函数通知函数中的参数,比如:将
@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.*(..))")
public void beforeMethod(Method method){
System.out.println("method before");
}
改为
@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.*(..))")
public void beforeMethod(){
System.out.println("method before");
}
[email protected]("execution(*
com.lqg.dao.user.AdminDaoImpl.*(..))")
public void aPointcut(){
}
Pointcut的定义的红色部分是空格,要不然出错,这个很奇怪啊。
4.aop在spring的配置
<!-- aop切面 -->
<bean id="happy" class="com.lqg.aop.AdminInterceptor"></bean>
同时要启动aspect的
<!-- 启动@Aspectj支持 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
5.juint的测试类
package com.lqg.dao;
import static org.junit.Assert.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.lqg.dao.user.AdminDao;
import com.lqg.memcache.MemCachedTool;
import com.lqg.model.user.Admin;
/**
* 閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷?
* @author QiuguoLi
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-common.xml")
public class DaoSupportTest {
@Autowired
private AdminDao adminDao;//DAO
@Autowired
private SessionFactory sessionFactory;
@Autowired
private MemCachedTool memcachedCache;
private Session session;
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {//閿熸枻鎷峰閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鎵ч敓鍙殑鍑ゆ嫹閿熸枻鎷?
//daoSupport=new DaoSupport<BookItem>();
//daoSupport=(DaoSupport<BookItem>)factory.getBean("bookItemDao");
session = SessionFactoryUtils.getSession(sessionFactory, true);
Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
}
@After
public void tearDown() throws Exception {//閿熸枻鎷烽敓锟介敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熻杈炬嫹姊板閿熸枻鎷烽敓锟?
adminDao=null;//閿熸枻鎷蜂浚閿熸枻鎷烽敓锟?
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(s, sessionFactory);
//SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);
}
@SuppressWarnings("unchecked")
@Test
public void testFindIntInt() {
//Map<String, String> orderby = new HashMap<String, String>(1);//閿熸枻鎷烽敓鏂ゆ嫹Map閿熸枻鎷烽敓鏂ゆ嫹
//orderby.put("createTime", "desc");//閿熸枻鎷烽敓鐭府鎷烽敓鏂ゆ嫹閿熸枻鎷锋椂閿熸垝鍊掗敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹
String username="admin";
String password="admin";
Admin admin= adminDao.login(username, password);
System.out.println(admin.getUsername()+" "+admin.getPassword());
assertNotNull("不是空的方法执行",admin);//閿熷彨璁规嫹find()閿熸枻鎷烽敓鏂ゆ嫹閿熻鍑ゆ嫹鏅掗敓鏂ゆ嫹閿熺獤顖ゆ嫹閿熻锟?
}
}
5.1要打开session,要不join的类不能换取
5.2 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-common.xml")记得要写进入