发表于: 2020-06-23 23:32:39
1 1616
今天完成的事情:
1. 在 service 里添加缓存
遇到的问题:
1. spring 报错 Could not resolve placeholder
原因:
我在多个配置文件中写了多个 <context:property-placeholder> 来引入 properties 文件
解决的方法:
因为我是 spring 3 以上的版本,可以通过加 ignore-unresolvable="true" 参数来解决,每一个都要加上。
<context:property-placeholder location="" ignore-unresolvable="true" />
2. 测试 service 中加缓存的时候报错
[ERROR] - com.schooner.MemCached.SchoonerSockIOPool.getSock(Unknown Source) - attempting to get SockIO from uninitialized pool!
原因:
在 web.xml 中配置 memcached.xml 并不会生效,需要在 spring 的配置文件里载入 memcached 的配置文件。
我还是不太清楚 web.xml 与 spring 配置文件的区别,一直以为把所有的配置都在 web.xml 里面引入就好了,需要再去查查资料。
解决的方法:
在 spring 配置文件里引入 memcached 的配置文件。
<import resource="classpath:memcached.xml"/>
贴一下 memcached 的配置:
memcached.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:memcached.properties" ignore-unresolvable="true"/>
<bean id="memcachedPool" class="com.whalin.MemCached.SockIOPool" factory-method="getInstance"
init-method="initialize" destroy-method="shutDown">
<!--这个值随意但是要与下方的 memcachedClient 相同-->
<constructor-arg>
<value>memcachedPool</value>
</constructor-arg>
<property name="servers">
<!--可以配置多个 server-->
<list>
<value>${memcached.server}</value>
</list>
</property>
<property name="failover" value="${memcached.failover}"/>
<property name="initConn" value="${memcached.initConn}"/>
<property name="minConn" value="${memcached.minConn}"/>
<property name="maxConn" value="${memcached.maxConn}"/>
<property name="maintSleep" value="${memcached.maintSleep}"/>
<property name="nagle" value="${memcached.nagle}"/>
<property name="socketTO" value="${memcached.socketTO}"/>
<property name="aliveCheck" value="${memcached.aliveCheck}"/>
</bean>
<bean id="memcachedClient" class="com.whalin.MemCached.MemCachedClient">
<constructor-arg>
<value>memcachedPool</value>
</constructor-arg>
</bean>
</beans>
memcached.properties
memcached.server = 127.0.0.1:11211
memcached.failover = true
memcached.initConn = 20
memcached.minConn = 10
memcached.maxConn = 40
memcached.maintSleep = 3000
memcached.nagle = false
memcached.socketTO = 3000
memcached.aliveCheck = true
单独测试 memcached 的单元测试(正常无报错):
package cn.mogeek.jsptiles.util;
import com.whalin.MemCached.MemCachedClient;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName MemcachedTest
* @Description memcached 测试
* @Author owlwinter
* @Date 2020/6/22 22:44
* @Version 1.0
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:memcached.xml"})
public class MemcachedTest {
@Resource(name = "memcachedClient")
private MemCachedClient memCachedClient;
@Test
public void testSerAndGet(){
Assert.assertNotNull(memCachedClient);
System.out.println(memCachedClient.keyExists("list"));
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
memCachedClient.set("strList", list);
List<String> result = (List<String>) memCachedClient.get("strList");
Assert.assertNotNull(result);
if (result != null && result.size() > 0){
for (String str :
result) {
System.out.println(str);
}
}
}
}
插入 service 的 MemcachedUtil.java
package cn.mogeek.util;
import com.whalin.MemCached.MemCachedClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Resource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
/**
* @ClassName MemcachedUtil
* @Description memcached 工具类
* @Author owlwinter
* @Date 2020/6/23 19:04
* @Version 1.0
**/
public class MemcachedUtil {
private static final Logger logger = LogManager.getLogger(MemcachedUtil.class);
private static MemCachedClient memCachedClient = null;
/*@Resource(name = "memcachedClient")
private static MemCachedClient memcachedClient;*/
static {
if (memCachedClient == null){
memCachedClient = new MemCachedClient("memcachedPool");
}
}
private MemcachedUtil(){}
/* code */
}
收获:
1. web.xml 与 spring 的配置文件有差异,需要学习一下
明天的计划:
1. 查 bug
评论