All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.cache.annotation.CachingConfigurationSelector Maven / Gradle / Ivy

There is a newer version: 6.1.6
Show newest version
/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cache.annotation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.AdviceModeImportSelector;
import org.springframework.context.annotation.AutoProxyRegistrar;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

/**
 * Selects which implementation of {@link AbstractCachingConfiguration} should
 * be used based on the value of {@link EnableCaching#mode} on the importing
 * {@code @Configuration} class.
 *
 * 

Detects the presence of JSR-107 and enables JCache support accordingly. * * @author Chris Beams * @author Stephane Nicoll * @since 3.1 * @see EnableCaching * @see ProxyCachingConfiguration */ public class CachingConfigurationSelector extends AdviceModeImportSelector { private static final String PROXY_JCACHE_CONFIGURATION_CLASS = "org.springframework.cache.jcache.config.ProxyJCacheConfiguration"; private static final String CACHE_ASPECT_CONFIGURATION_CLASS_NAME = "org.springframework.cache.aspectj.AspectJCachingConfiguration"; private static final String JCACHE_ASPECT_CONFIGURATION_CLASS_NAME = "org.springframework.cache.aspectj.AspectJJCacheConfiguration"; private static final boolean jsr107Present; private static final boolean jcacheImplPresent; static { ClassLoader classLoader = CachingConfigurationSelector.class.getClassLoader(); jsr107Present = ClassUtils.isPresent("javax.cache.Cache", classLoader); jcacheImplPresent = ClassUtils.isPresent(PROXY_JCACHE_CONFIGURATION_CLASS, classLoader); } /** * Returns {@link ProxyCachingConfiguration} or {@code AspectJCachingConfiguration} * for {@code PROXY} and {@code ASPECTJ} values of {@link EnableCaching#mode()}, * respectively. Potentially includes corresponding JCache configuration as well. */ @Override public String[] selectImports(AdviceMode adviceMode) { return switch (adviceMode) { case PROXY -> getProxyImports(); case ASPECTJ -> getAspectJImports(); }; } /** * Return the imports to use if the {@link AdviceMode} is set to {@link AdviceMode#PROXY}. *

Take care of adding the necessary JSR-107 import if it is available. */ private String[] getProxyImports() { List result = new ArrayList<>(3); result.add(AutoProxyRegistrar.class.getName()); result.add(ProxyCachingConfiguration.class.getName()); if (jsr107Present && jcacheImplPresent) { result.add(PROXY_JCACHE_CONFIGURATION_CLASS); } return StringUtils.toStringArray(result); } /** * Return the imports to use if the {@link AdviceMode} is set to {@link AdviceMode#ASPECTJ}. *

Take care of adding the necessary JSR-107 import if it is available. */ private String[] getAspectJImports() { List result = new ArrayList<>(2); result.add(CACHE_ASPECT_CONFIGURATION_CLASS_NAME); if (jsr107Present && jcacheImplPresent) { result.add(JCACHE_ASPECT_CONFIGURATION_CLASS_NAME); } return StringUtils.toStringArray(result); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy