最近想在springboot项目中集成memcached,网上查看的资料都是基于配置文件形式的,所以想看看如何基于注解方式来使用呢?还有一个问题就是
@Override @ReadThroughSingleCache(namespace = "demo", expiration = 30000) public Person selectById(Long id) { System.out.println("person 缓存未命中"); return personMapper.selectById(id); }
如何让 @ReadThroughSingleCache
注解生效呢?
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <import resource="simplesm-context.xml" /> <aop:aspectj-autoproxy /> <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory"> <property name="cacheClientFactory"> <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /> </property> <property name="addressProvider"> <bean class="com.google.code.ssm.config.DefaultAddressProvider"> <property name="address" value="127.0.0.1:11211" /> </bean> </property> <property name="configuration"> <bean class="com.google.code.ssm.providers.CacheConfiguration"> <property name="consistentHashing" value="true" /> </bean> </property> </bean> </beans>
以上是官方的使用示例。
导入配置 spring-boot Importing XML configuration
通过@Bean
定义对象 官方文档
如
@Configuration public class AppConfig { @Bean public TransferService transferService() { return new TransferServiceImpl(); } }