`

知识积累--SSM在Web工程中的运用

 
阅读更多

SSM(Simple Spring Memcached)--com\google\code\simple-spring-memcached\simple-spring-memcached\3.6.0\simple-spring-memcached-3.6.0.jar

其实我也只是用到了里面的一个CacheFactory,获取链接缓存的客户端com.google.code.ssm.Cache 中的incr自增方法

 

Maven中坐标:

 

<dependency> 
	<groupId>com.google.code.simple-spring-memcached</groupId> 
	<artifactId>xmemcached-provider</artifactId>
</dependency>
<!-- 用这个是因为上面的jar包依赖这个,其实单独用下面这个也是完全可以的 -->
<dependency>
	<groupId>com.googlecode.xmemcached</groupId>
	<artifactId>xmemcached</artifactId>
</dependency>

 spring配置文件中的配置:

 

 注意1:simplesm-context.xml 是simple-spring-memcached中的,所以你直接引用就好。

 注意2:上图中的红色标记中的“2”“3”一定要加上去,不然下面就会报错误的。

 注意3:xmlns:cache=...的命名空间记得在spring的主配置文件的头部加上去。

 

 

<!--spring主配置 有关缓存的关键配置-->
<import resource="classpath:simplesm-context.xml" />
<!-- 启用缓存注解 -->
<bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />
<cache:annotation-driven cache-manager="cacheManager"/>
<aop:aspectj-autoproxy />

<!-- simple-spring-memcache -->
<bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
	<property name="cacheClientFactory">
		<bean name="cacheClientFactory"
			  class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
	</property>
	<!-- 定义了缓存节点的IP地址和端口号 -->
	<property name="addressProvider">
		<bean class="com.google.code.ssm.config.DefaultAddressProvider">
			<property name="address" value="${memcached.servers}" />
		</bean>
	</property>
	<!-- 定义了缓存节点的查找方法 -->
	<property name="configuration">
		<bean class="com.google.code.ssm.providers.CacheConfiguration">
			<property name="consistentHashing" value="true" />
		</bean>
	</property>
</bean>

 memcached.properties配置:

 

 

#逗号隔开的是两个节点,他们只主备关系,一组主备与另一组主备 空格隔开,没有集群和主备单节点也是可以的
memcached.servers=X.X.X.X:11221,X1.X1.X1.X1:12221 A.A.A.A:11221,A1.A1.A1.A1:12221
memcached.connectionPoolSize=10
memcached.failureMode=true
memcached.connectTimeout=1000
memcached.timeout=1000
memcached.exp=600
memcached.createOrder.exp=1800

 

 

使用:

举例:我是springMVC ,因为只是简单的用到incr自增的方法,做一个简单的逻辑,我就直接写在controller中了

 

//这个如果不写的话,项目启动的时候会报错,空指针,cacheBase无法注入到
defaultmemcachedClient,具体见错误2

@Autowired
private CacheBase cacheBase;
@Autowired
private CacheFactory defaultmemcachedClient;

 

*上面需要注意的是:

    1:CacheBase也要注入

    2:上面的一定要和配置文件中的保持你一致(cacheBase看的源码的,defaultmemcachedClient与上面的配置bean的name保持一致)

 

 获取客户端对象操作memcached:

 

//根据key获取缓存中的值
Long counts=defaultmemcachedClient.getCache().getCounter("你自定义的KEY");
//自增
defaultmemcachedClient.getCache().incr(key, delta[自增幅度], 初始值, 有效期(s));

 *说明:

       1:getCounter可以获取incr(自增),或者decr(自减)的key的缓存的值

       2:incr 的机制,根据key查缓存值,有的话,在现有的基础上加上增长服务,没有的话返回初始值,并将初始值存放到缓存中

 

写单元测试遇到的问题:

  错误1:

 

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.google.code.ssm.aop.CacheBase] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 

 

应对措施:

1:在资源文件下放从源码包中的拉出来的文件:simplesm-context.xml

2:单元测试加载这个文件,如下

 

然后单元测试就不在报错了,但是当应用整个在tomcat服务器跑起来的时候,依然会报上面的错误,错误的原因是spring中主文件配置中的2,3没有写进去,无法初始化要使用的CacheBase,导致初始化的memcacehdClient报错。

 

上面的“2”“3”,加上去可以解决“错误1”,但是再次启动tomcat跑应用的时候,再次报错,具体错误如下

错误2:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'broadbandController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.google.code.ssm.CacheFactory com.chinatelecom.web.bss.controller.business.BroadbandController.defaultmemcachedClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultMemcachedClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.google.code.ssm.aop.CacheBase com.google.code.ssm.CacheFactory.cacheBase; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheBase' defined in class path resource [simplesm-context.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultMemcachedClient': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException

 应对这个错误,上上面代码注释上有说明,具体为什么还需要,进一步研究,后续补充.....

 

部分源码如下:



 

 

 

虽然解决了我使用的问题,但是还是有些不明白的地方,后续会对上面没说明白的地方进行补充.

 

 

 

~~~~~~~~纯个人项目遇到的问题总结,如能帮上您,我很开心,如有错误或者不严谨的地方,还望指教!~~~

 

 

 

 

 

  • 大小: 23.8 KB
  • 大小: 73.2 KB
  • 大小: 12.4 KB
  • 大小: 24.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics