Springcache 不僅適用於 XML 配置

我正在用XML配置实现Springcache,并希望摆脱所有的注释。我只想替换应用程序Context.xml文件中的注释。这是我的代码 -


//DummyBean.java

package com.spring.example;

interface DummyBean {

    public String getCity();

}


//DummyBeanImpl.java

package 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.java


package 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();

  }

}


胡子哥哥
浏览 93回答 1
1回答

12345678_0001

使用 XML 配置的各个方面时,您还需要添加一个部分来应用该方面。aop:config<aop:config> &nbsp;&nbsp;&nbsp;&nbsp;<aop:advisor&nbsp;advice-ref="cacheAdvice"&nbsp;pointcut="execution(*&nbsp;com.spring.example.DummyBean+.*(..))"/> </aop:config>假设是一个由此实现的接口,应该可以解决问题。这表示您要将(方面)应用于与表达式匹配的 Bean。DummyBeancom.spring.example.DummyBeanImplcacheAdvice另请参阅参考指南
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java