Skip to content

Commit 06d9db1

Browse files
committed
Introduce CachingBeanRegistrationAotProcessor
It contributes proxy hints for interfaces when class proxying is not forced. Closes gh-28943
1 parent 491b777 commit 06d9db1

File tree

3 files changed

+206
-1
lines changed

3 files changed

+206
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cache.annotation;
18+
19+
import java.lang.annotation.Annotation;
20+
import java.lang.reflect.AnnotatedElement;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
23+
24+
import org.springframework.aop.config.AopConfigUtils;
25+
import org.springframework.aop.framework.AopProxyUtils;
26+
import org.springframework.aot.hint.RuntimeHints;
27+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
28+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
29+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30+
import org.springframework.beans.factory.support.RegisteredBean;
31+
import org.springframework.core.annotation.AnnotationUtils;
32+
import org.springframework.core.annotation.MergedAnnotations;
33+
import org.springframework.util.ClassUtils;
34+
import org.springframework.util.ReflectionUtils;
35+
36+
/**
37+
* {@link BeanRegistrationAotProcessor} to register runtime hints for beans that use caching annotations to
38+
* enable JDK proxy creation when needed.
39+
*
40+
* @author Sebastien Deleuze
41+
* @since 6.0
42+
*/
43+
public class CachingBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
44+
45+
private static final Set<Class<? extends Annotation>> CACHE_OPERATION_ANNOTATIONS = new LinkedHashSet<>(8);
46+
47+
static {
48+
CACHE_OPERATION_ANNOTATIONS.add(Cacheable.class);
49+
CACHE_OPERATION_ANNOTATIONS.add(CacheEvict.class);
50+
CACHE_OPERATION_ANNOTATIONS.add(CachePut.class);
51+
CACHE_OPERATION_ANNOTATIONS.add(Caching.class);
52+
}
53+
54+
@Override
55+
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
56+
if (isCaching(registeredBean.getBeanClass()) && !isClassProxyingForced(registeredBean.getBeanFactory())) {
57+
return (generationContext, beanRegistrationCode) -> registerSpringProxy(registeredBean.getBeanClass(),
58+
generationContext.getRuntimeHints());
59+
}
60+
return null;
61+
}
62+
63+
private static boolean isClassProxyingForced(ConfigurableListableBeanFactory beanFactory) {
64+
return beanFactory.containsBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME) &&
65+
Boolean.TRUE.equals(beanFactory.getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)
66+
.getPropertyValues().get("proxyTargetClass"));
67+
}
68+
69+
private boolean isCaching(Class<?> beanClass) {
70+
if (!AnnotationUtils.isCandidateClass(beanClass, CACHE_OPERATION_ANNOTATIONS)) {
71+
return false;
72+
}
73+
Set<AnnotatedElement> elements = new LinkedHashSet<>();
74+
elements.add(beanClass);
75+
ReflectionUtils.doWithMethods(beanClass, elements::add);
76+
for (Class<?> interfaceClass : ClassUtils.getAllInterfacesForClass(beanClass)) {
77+
elements.add(interfaceClass);
78+
ReflectionUtils.doWithMethods(interfaceClass, elements::add);
79+
}
80+
return elements.stream().anyMatch(element -> {
81+
MergedAnnotations mergedAnnotations = MergedAnnotations.from(element, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
82+
return CACHE_OPERATION_ANNOTATIONS.stream().anyMatch(mergedAnnotations::isPresent);
83+
});
84+
}
85+
86+
private static void registerSpringProxy(Class<?> type, RuntimeHints runtimeHints) {
87+
Class<?>[] proxyInterfaces = ClassUtils.getAllInterfacesForClass(type);
88+
if (proxyInterfaces.length == 0) {
89+
return;
90+
}
91+
runtimeHints.proxies().registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(proxyInterfaces));
92+
}
93+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor= \
2-
org.springframework.context.aot.ReflectiveProcessorBeanRegistrationAotProcessor
2+
org.springframework.context.aot.ReflectiveProcessorBeanRegistrationAotProcessor,\
3+
org.springframework.cache.annotation.CachingBeanRegistrationAotProcessor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cache.annotation;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aop.config.AopConfigUtils;
22+
import org.springframework.aop.framework.AopProxyUtils;
23+
import org.springframework.aot.generate.GenerationContext;
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
26+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
27+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
28+
import org.springframework.beans.factory.aot.BeanRegistrationCode;
29+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
30+
import org.springframework.beans.factory.support.RegisteredBean;
31+
import org.springframework.beans.factory.support.RootBeanDefinition;
32+
import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
33+
import org.springframework.lang.Nullable;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.mockito.Mockito.mock;
37+
38+
/**
39+
* Tests for {@link CachingBeanRegistrationAotProcessor}.
40+
*
41+
* @author Sebastien Deleuze
42+
*/
43+
public class CachingBeanRegistrationAotProcessorTests {
44+
45+
BeanRegistrationAotProcessor processor = new CachingBeanRegistrationAotProcessor();
46+
47+
GenerationContext generationContext = new TestGenerationContext();
48+
49+
50+
@Test
51+
void ignoresNonCachingBean() {
52+
assertThat(createContribution(NonCaching.class, false)).isNull();
53+
}
54+
55+
@Test
56+
void contributesProxyForCacheableInterface() {
57+
process(CacheableServiceImpl.class, false);
58+
RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
59+
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AopProxyUtils.completeJdkProxyInterfaces(CacheableServiceInterface.class))).accepts(runtimeHints);
60+
}
61+
62+
@Test
63+
void ignoresProxyForCacheableInterfaceWithClassProxying() {
64+
assertThat(createContribution(CacheableServiceImpl.class, true)).isNull();
65+
}
66+
67+
@Test
68+
void ignoresProxyForCacheableClass() {
69+
assertThat(createContribution(CacheableService.class, true)).isNull();
70+
}
71+
72+
@Nullable
73+
private BeanRegistrationAotContribution createContribution(Class<?> beanClass, boolean forceClassProxying) {
74+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
75+
if (forceClassProxying) {
76+
AopConfigUtils.registerAutoProxyCreatorIfNecessary(beanFactory);
77+
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(beanFactory);
78+
}
79+
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
80+
return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
81+
}
82+
83+
private void process(Class<?> beanClass, boolean forceClassProxying) {
84+
BeanRegistrationAotContribution contribution = createContribution(beanClass, forceClassProxying);
85+
assertThat(contribution).isNotNull();
86+
contribution.applyTo(this.generationContext, mock(BeanRegistrationCode.class));
87+
}
88+
89+
static class NonCaching {
90+
}
91+
92+
interface CacheableServiceInterface {
93+
94+
@Cacheable
95+
void invoke();
96+
}
97+
98+
class CacheableServiceImpl implements CacheableServiceInterface {
99+
100+
@Override
101+
public void invoke() {
102+
}
103+
}
104+
105+
class CacheableService {
106+
107+
@Cacheable
108+
public void invoke() {
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)