MVC中的Spring Cache-可以通过自动装配进行查找吗?

我看到当应用程序启动时,我的单例缓存已创建

  

DEBUG创建CGLIB代理:目标源是SingletonTargetsource,用于   目标对象[com.abc.xyz.util.CacheUtil@14e3dd3]调试无法   将任何优化应用于建议的方法:public java.util.Map

但是当我尝试如何使用自动装配来查找值时,它不会达到创建的单例并创建CacheUtil的新实例。

CacheUtil.java [此类使用@Component注释]

    public Map getSelectOptions(String codeType) {
        System.out.println("Cache Breached!!!");
        HashMap selectOpts = new HashMap();
        Vector<MyTableDO> vCodeMap = null;
        vCodeMap = MyTableDO.getcodesFromDatabase(codeType,"LookupCacheUtil"); 


        if(vCodeMap == null || vCodeMap.size() == 0) return selectOpts;

        vCodeMap.forEach(codeMap -> selectOpts.put(codeMap.getcodeValue(),codeMap.getcodeDesc()));

        return selectOpts;
    }

我的spring配置xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.abc.xyz" />
    <context:annotation-config />
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

   <bean id="cacheUtil" class="com.abc.xyz.util.CacheUtil" />
</beans>

类调用缓存的方法

  @Autowired
  @Qualifier("cacheUtil")
  protected CacheUtil cacheUtil;

  public Map getSelectOptions(String codeType) {

      AnnotationconfigApplicationContext ctx = new AnnotationconfigApplicationContext(MyApplication.class); 
      //ctx.refresh();
      CacheUtil lkp = (CacheUtil) ctx.getBean(CacheUtil.class); 
      ctx.close();   

      System.out.println("App Context lookupCacheUtil -"+lkp); // Not the same object of Spring Cache and comes to be new instance on every call
      System.out.println("Autowired lookupCacheUtil -"+cacheUtil); // Always comes to be NULL

      return lkp.getSelectOptions(codeType);  
  }

}

MyApplication类

@SpringBootApplication
@EnableCaching
public class MyApplication extends SpringBootServletInitializer{


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);
    }

    @Override
    public void onStartup(ServletContext container) {
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setConfigLocation("/WEB-INF/config/spring-servlet.xml");

        //using servlet 3 api to dynamically create spring dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("spring",new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(2);
        dispatcher.addMapping("/");
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }

}
happyboy_1 回答:MVC中的Spring Cache-可以通过自动装配进行查找吗?

经过详细分析,我对Autowired的理解更加完善。感谢link

在我的情况下,我已经将'CacheUtil'自动连接到了一个form bean。看来,Bean并没有在春季或至少在这种情况下得到管理。相同的自动接线功能在由Spring管理的控制器中正常工作。

因此,我通过从应用程序上下文中获取CacheUtil的Spring Cache“代理”版本来解决。下面的代码片段应该有所帮助(方法getInstance()):

import org.springframework.beans.BeansException;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component("MyCache")
public class CacheUtil implements ApplicationContextAware{

    private static ApplicationContext appContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // TODO Auto-generated method stub
        appContext = applicationContext;
    }
    /**
     * Method to fetch the shared instance of the Spring Cache Object that helps reach the 
     * Cache that resides in Application Context.
     * 
     * @return Singleton shared instance of the Spring Cache Proxy of this class CacheUtil 
     */
    public static CacheUtil getInstance() {
        CacheUtil appCache = appContext.getBean("MyCache",CacheUtil.class);        
        if(appCache != null) return appCache;

        return new CacheUtil();
    }
本文链接:https://www.f2er.com/3067838.html

大家都在问