Java Spring缓存代理不应用于xml中加载的bean与@Configuration中加载的bean
是否仍然可以通过Java Spring缓存代理不应用于xml中加载的bean与@Configuration中加载的bean,java,spring,caching,spring-annotations,applicationcontext,Java,Spring,Caching,Spring Annotations,Applicationcontext,是否仍然可以通过将bean加载到xml文件中,由@Coniguration注释类代理,该类具有@EnableCaching并声明SimpleCache Manager?对于我正在使用的大型应用程序来说,这是最简单的方法,我的最终偏好是将其全部转换为配置类,但这需要做更多的工作,并且几乎没有针对该应用程序的单元测试-,有些东西会完全崩溃。另一个选择是在xml中声明缓存,这很好,但我觉得这是一个倒退 注意:是在一个单独的xml文件“integration.xml”中声明的,我把它放回applicat
将bean加载到xml文件中,由@Coniguration
注释类代理,该类具有@EnableCaching
并声明SimpleCache Manager
?对于我正在使用的大型应用程序来说,这是最简单的方法,我的最终偏好是将其全部转换为配置类,但这需要做更多的工作,并且几乎没有针对该应用程序的单元测试-,有些东西会完全崩溃。另一个选择是在xml中声明缓存,这很好,但我觉得这是一个倒退
注意:
是在一个单独的xml文件“integration.xml”中声明的,我把它放回applicationContext.xml中,但它不影响任何内容
缓存的声明和通过@EnableCaching
启用缓存的操作在一段时间前被移到了下面的java类中,我想没有人注意到它停止了工作。所以我想让它以最好的方式再次工作
应用程序上下文(为简洁而编辑)
编辑:修复了复制粘贴重新格式化的错误
和@EnableCaching是相等的。您只能有一个(可能这是您的问题的根源)。您能提供实际使用缓存的代码示例吗?哪个bean应该使用缓存特性
chalimartines提供的答案嘿,你已经写好了。应该启用缓存。我注意到的另一件事是MethodCachingConfiguration的包声明(net.adl.config)在组件扫描中丢失了。噢,“启用缓存”是一个输入错误。我试图清理我的测试混乱,看看顺序是否重要。包裹确实会被扫描,我试着重新命名一些东西,因为这些名字对我不时为之工作的公司来说非常具体。现在修复和@EnableCaching是相等的,您只能有一个(可能是您的问题的根源),您能提供实际使用缓存的代码示例吗?哪个bean应该使用缓存功能。这很有效,我不知道为什么我没有想到我已经使用了两次,哈哈,谢谢。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
"
>
<util:properties id="properties" location="classpath:config.properties"/>
<context:property-placeholder properties-ref="properties"/>
<!-- TODO: Replace this with MethodInvokingBean - we don't actually *want* a ConfigFactory instance -->
<bean id="configFactory" class="net.adl.service.ConfigFactory">
<property name="properties" ref="properties" />
</bean>
<!-- Enable Caching -->
<cache:annotation-driven proxy-target-class="true"/>
<!-- Declaring the cache manager and caches here works, but I feel is a step backwards to put them back in the XML config -->
<context:component-scan base-package="
net.adl.quartz,
net.adl.config,
net.adl.dao,
net.adl.service,
net.adl.audit,
net.adl.diagnostic,
net.adl.loader,
net.adl.loader"/>
<!-- add support for @Scheduled -->
<task:scheduler id="taskScheduler" pool-size="10"/>
<task:executor id="taskExecutor" pool-size="10"/>
<!-- Used for real time monitoring of folders for data loads -->
<task:executor id="realTimeAutoLoaderExecutor" pool-size="1"/>
<task:annotation-driven scheduler="taskScheduler" executor="taskExecutor"/>
<!-- enable @Transactional annotations -->
<bean id="transactionAdvice" class="net.adl.aop.TransactionAdvice"/>
<!--<bean id="profiler" class="net.adl.util.Profiler"/>-->
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="transactionAdvice"/>
<!--<aop:include name="profiler"/>-->
</aop:aspectj-autoproxy>
<!-- set system properties -->
<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!--
"systemProperties" is predefined; see:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-beandef-xml-based
-->
<property name="targetObject" value="#{@systemProperties}"/>
<property name="targetMethod" value="putAll"/>
<property name="arguments">
<util:properties>
<prop key="net.sf.ehcache.skipUpdateCheck">true</prop>
<prop key="org.terracotta.quartz.skipUpdateCheck">true</prop>
</util:properties>
</property>
</bean>
<!-- Exception translation bean post processor -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<bean id="versionInfo" class="net.adl.util.VersionInfo">
<property name="versionFilePath" value="${labmatrix.home}/version-info.txt"/>
</bean>
<!-- Here is where we call in the <context:annotation-config/>, not sure why its done in a separate file -->
<import resource="resources/spring/integration.xml"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-3.1.xsd
"
>
<context:annotation-config/>
</beans>
package net.adl.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
@Configuration
@EnableCaching
public class MethodCacheConfiguration {
public static final String STUDY_CONFIG = "config.studies";
public static final String ACCESS_CONFIG = "config.access";
public static final String WORKFLOW_CONFIG = "config.workflows";
public static final String PROCESS_CONFIG = "config.processes";
public static final String QUERY_CONFIG = "config.queries";
public static final String AUTOLOADER_CONFIG = "config.autoloader";
public static final String LOCALIZATION = "localization";
public static final String FACTORY_CONFIG = "config.factories";
/**
* Configures the cacheManager bean for @Cacheable annotation support
*/
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(
new ConcurrentMapCache(STUDY_CONFIG),
new ConcurrentMapCache(ACCESS_CONFIG),
new ConcurrentMapCache(WORKFLOW_CONFIG),
new ConcurrentMapCache(PROCESS_CONFIG),
new ConcurrentMapCache(QUERY_CONFIG),
new ConcurrentMapCache(AUTOLOADER_CONFIG),
new ConcurrentMapCache(LOCALIZATION),
new ConcurrentMapCache(FACTORY_CONFIG)
));
return cacheManager;
}
}