我正在用XML配置实现Springcache,并希望摆脱所有的注释。我只想替换应用程序Context.xml文件中的注释。这是我的代码 -//DummyBean.javapackage com.spring.example;interface DummyBean { public String getCity();}//DummyBeanImpl.javapackage com.spring.example;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.Cacheable;import org.springframework.cache.annotation.EnableCaching;@EnableCaching@CacheConfig(cacheNames={"myCache"})public class DummyBeanImpl implements DummyBean { private String city = "New York"; @Cacheable() public String getCity() { System.out.println("DummyBean.getCity() called!"); return city; }}SpringAwareAnnotationXMLConfig.javapackage com.spring.example;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.GenericXmlApplicationContext;public class SpringAwareAnnotationXMLConfig { public static void main(String[] args) throws Exception { AbstractApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); DummyBean personService = (DummyBean) context.getBean("myBean"); System.out.println(personService.getCity()); System.out.println(personService.getCity()); System.out.println(personService.getCity()); ((AbstractApplicationContext) context).close(); }}
1 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
使用 XML 配置的各个方面时,您还需要添加一个部分来应用该方面。aop:config
<aop:config> <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.spring.example.DummyBean+.*(..))"/> </aop:config>
假设是一个由此实现的接口,应该可以解决问题。这表示您要将(方面)应用于与表达式匹配的 Bean。DummyBean
com.spring.example.DummyBeanImpl
cacheAdvice
另请参阅参考指南
添加回答
举报
0/150
提交
取消