Skip to content

Commit 0a423ac

Browse files
mp911dechristophstrobl
authored andcommitted
Open up AuditingBeanDefinitionRegistrarSupport.registerAuditHandlerBeanDefinition(…) to allow additional bean registrations.
Original Pull Request: #2624
1 parent 4e23153 commit 0a423ac

30 files changed

+710
-548
lines changed

src/main/java/org/springframework/data/aot/AotContext.java

Lines changed: 154 additions & 156 deletions
Large diffs are not rendered by default.

src/main/java/org/springframework/data/aot/AotRepositoryInformation.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@
2525
import org.springframework.data.repository.core.RepositoryInformationSupport;
2626
import org.springframework.data.repository.core.RepositoryMetadata;
2727
import org.springframework.data.repository.core.support.RepositoryFragment;
28-
import org.springframework.lang.NonNull;
2928

3029
/**
3130
* {@link RepositoryInformation} based on {@link RepositoryMetadata} collected at build time.
3231
*
3332
* @author Christoph Strobl
34-
* @see org.springframework.data.repository.core.RepositoryInformation
35-
* @see org.springframework.data.repository.core.RepositoryMetadata
3633
* @since 3.0
3734
*/
3835
class AotRepositoryInformation extends RepositoryInformationSupport implements RepositoryInformation {
@@ -47,20 +44,19 @@ class AotRepositoryInformation extends RepositoryInformationSupport implements R
4744
}
4845

4946
@Override
50-
public boolean isCustomMethod(@NonNull Method method) {
47+
public boolean isCustomMethod(Method method) {
5148
// TODO:
5249
return false;
5350
}
5451

5552
@Override
56-
public boolean isBaseClassMethod(@NonNull Method method) {
53+
public boolean isBaseClassMethod(Method method) {
5754
// TODO
5855
return false;
5956
}
6057

61-
@NonNull
6258
@Override
63-
public Method getTargetClassMethod(@NonNull Method method) {
59+
public Method getTargetClassMethod(Method method) {
6460
// TODO
6561
return method;
6662
}
@@ -69,7 +65,6 @@ public Method getTargetClassMethod(@NonNull Method method) {
6965
* @return configured repository fragments.
7066
* @since 3.0
7167
*/
72-
@NonNull
7368
public Set<RepositoryFragment<?>> getFragments() {
7469
return new LinkedHashSet<>(fragments.get());
7570
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 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+
package org.springframework.data.aot;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Optional;
22+
23+
import org.springframework.beans.factory.BeanFactory;
24+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25+
import org.springframework.beans.factory.config.BeanDefinition;
26+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
27+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
28+
import org.springframework.beans.factory.support.RootBeanDefinition;
29+
import org.springframework.util.ClassUtils;
30+
31+
/**
32+
* Default {@link AotContext} implementation.
33+
*
34+
* @author Mark Paluch
35+
* @since 3.0
36+
*/
37+
class DefaultAotContext implements AotContext {
38+
39+
private final ConfigurableListableBeanFactory factory;
40+
41+
public DefaultAotContext(BeanFactory beanFactory) {
42+
factory = beanFactory instanceof ConfigurableListableBeanFactory ? (ConfigurableListableBeanFactory) beanFactory
43+
: new DefaultListableBeanFactory(beanFactory);
44+
}
45+
46+
@Override
47+
public ConfigurableListableBeanFactory getBeanFactory() {
48+
return factory;
49+
}
50+
51+
@Override
52+
public TypeIntrospector introspectType(String typeName) {
53+
return new DefaultTypeIntrospector(typeName);
54+
}
55+
56+
@Override
57+
public IntrospectedBeanDefinition introspectBeanDefinition(String beanName) {
58+
return new DefaultIntrospectedBeanDefinition(beanName);
59+
}
60+
61+
class DefaultTypeIntrospector implements TypeIntrospector {
62+
63+
private final String typeName;
64+
65+
DefaultTypeIntrospector(String typeName) {
66+
this.typeName = typeName;
67+
}
68+
69+
@Override
70+
public boolean isTypePresent() {
71+
return ClassUtils.isPresent(typeName, getClassLoader());
72+
}
73+
74+
@Override
75+
public Class<?> resolveRequiredType() throws TypeNotPresentException {
76+
try {
77+
return ClassUtils.forName(typeName, getClassLoader());
78+
} catch (ClassNotFoundException cause) {
79+
throw new TypeNotPresentException(typeName, cause);
80+
}
81+
}
82+
83+
@Override
84+
public Optional<Class<?>> resolveType() {
85+
return isTypePresent() ? Optional.of(resolveRequiredType()) : Optional.empty();
86+
}
87+
88+
@Override
89+
public boolean hasBean() {
90+
return !getBeanNames().isEmpty();
91+
}
92+
93+
@Override
94+
public List<String> getBeanNames() {
95+
return isTypePresent() ? Arrays.asList(factory.getBeanNamesForType(resolveRequiredType()))
96+
: Collections.emptyList();
97+
}
98+
}
99+
100+
class DefaultIntrospectedBeanDefinition implements IntrospectedBeanDefinition {
101+
102+
private final String beanName;
103+
104+
DefaultIntrospectedBeanDefinition(String beanName) {
105+
this.beanName = beanName;
106+
}
107+
108+
@Override
109+
public boolean isPresent() {
110+
return factory.containsBeanDefinition(beanName);
111+
}
112+
113+
@Override
114+
public boolean isFactoryBean() {
115+
return factory.isFactoryBean(beanName);
116+
}
117+
118+
@Override
119+
public BeanDefinition getBeanDefinition() throws NoSuchBeanDefinitionException {
120+
return factory.getBeanDefinition(beanName);
121+
}
122+
123+
@Override
124+
public RootBeanDefinition getRootBeanDefinition() throws NoSuchBeanDefinitionException {
125+
BeanDefinition beanDefinition = getBeanDefinition();
126+
127+
if (beanDefinition instanceof RootBeanDefinition rootBeanDefinition) {
128+
return rootBeanDefinition;
129+
}
130+
131+
throw new IllegalStateException(String.format("%s is not a root bean", beanName));
132+
}
133+
134+
@Override
135+
public Class<?> resolveType() {
136+
return factory.getType(beanName, false);
137+
}
138+
}
139+
}

src/main/java/org/springframework/data/aot/DefaultAotRepositoryContext.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,21 @@
3535
*/
3636
class DefaultAotRepositoryContext implements AotRepositoryContext {
3737

38-
private AotContext aotContext;
39-
38+
private final AotContext aotContext;
4039
private final Lazy<Set<MergedAnnotation<Annotation>>> resolvedAnnotations = Lazy.of(this::discoverAnnotations);
4140
private final Lazy<Set<Class<?>>> managedTypes = Lazy.of(this::discoverTypes);
4241

4342
private RepositoryInformation repositoryInformation;
44-
4543
private Set<String> basePackages;
4644
private Set<Class<? extends Annotation>> identifyingAnnotations;
47-
4845
private String beanName;
4946

50-
public AotContext getAotContext() {
51-
return aotContext;
47+
public DefaultAotRepositoryContext(AotContext aotContext) {
48+
this.aotContext = aotContext;
5249
}
5350

54-
public void setAotContext(AotContext aotContext) {
55-
this.aotContext = aotContext;
51+
public AotContext getAotContext() {
52+
return aotContext;
5653
}
5754

5855
@Override
@@ -106,6 +103,16 @@ public Set<Class<?>> getResolvedTypes() {
106103
return managedTypes.get();
107104
}
108105

106+
@Override
107+
public TypeIntrospector introspectType(String typeName) {
108+
return aotContext.introspectType(typeName);
109+
}
110+
111+
@Override
112+
public IntrospectedBeanDefinition introspectBeanDefinition(String beanName) {
113+
return aotContext.introspectBeanDefinition(beanName);
114+
}
115+
109116
protected Set<MergedAnnotation<Annotation>> discoverAnnotations() {
110117

111118
Set<MergedAnnotation<Annotation>> annotations = getResolvedTypes().stream()
@@ -127,7 +134,8 @@ protected Set<Class<?>> discoverTypes() {
127134

128135
if (!getIdentifyingAnnotations().isEmpty()) {
129136

130-
Set<Class<?>> classes = aotContext.getTypeScanner().scanPackages(getBasePackages()).forTypesAnnotatedWith(getIdentifyingAnnotations()).collectAsSet();
137+
Set<Class<?>> classes = aotContext.getTypeScanner().scanPackages(getBasePackages())
138+
.forTypesAnnotatedWith(getIdentifyingAnnotations()).collectAsSet();
131139
types.addAll(TypeCollector.inspect(classes).list());
132140
}
133141

src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
2324
import org.springframework.aot.generate.GenerationContext;
2425
import org.springframework.beans.factory.BeanFactory;
2526
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
2627
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
2728
import org.springframework.beans.factory.support.RegisteredBean;
2829
import org.springframework.core.ResolvableType;
2930
import org.springframework.data.domain.ManagedTypes;
30-
import org.springframework.lang.NonNull;
3131
import org.springframework.lang.Nullable;
3232
import org.springframework.util.ClassUtils;
3333
import org.springframework.util.StringUtils;
@@ -38,8 +38,6 @@
3838
*
3939
* @author Christoph Strobl
4040
* @author John Blum
41-
* @see org.springframework.beans.factory.BeanFactoryAware
42-
* @see org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
4341
* @since 3.0
4442
*/
4543
public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@@ -48,7 +46,7 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio
4846
@Nullable private String moduleIdentifier;
4947

5048
@Override
51-
public BeanRegistrationAotContribution processAheadOfTime(@NonNull RegisteredBean registeredBean) {
49+
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
5250

5351
if (!isMatch(registeredBean.getBeanClass(), registeredBean.getBeanName())) {
5452
return null;

src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotContribution.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.beans.factory.aot.BeanRegistrationCode;
2424
import org.springframework.core.ResolvableType;
2525
import org.springframework.data.domain.ManagedTypes;
26-
import org.springframework.lang.NonNull;
26+
import org.springframework.lang.Nullable;
2727

2828
/**
2929
* {@link BeanRegistrationAotContribution} used to contribute a {@link ManagedTypes} registration.
@@ -38,8 +38,8 @@ public class ManagedTypesRegistrationAotContribution implements BeanRegistration
3838
private final ManagedTypes managedTypes;
3939
private final BiConsumer<ResolvableType, GenerationContext> contributionAction;
4040

41-
public ManagedTypesRegistrationAotContribution(AotContext aotContext, ManagedTypes managedTypes,
42-
@NonNull BiConsumer<ResolvableType, GenerationContext> contributionAction) {
41+
public ManagedTypesRegistrationAotContribution(AotContext aotContext, @Nullable ManagedTypes managedTypes,
42+
BiConsumer<ResolvableType, GenerationContext> contributionAction) {
4343

4444
this.aotContext = aotContext;
4545
this.managedTypes = managedTypes;
@@ -50,14 +50,12 @@ protected AotContext getAotContext() {
5050
return this.aotContext;
5151
}
5252

53-
@NonNull
5453
protected ManagedTypes getManagedTypes() {
5554
return managedTypes == null ? ManagedTypes.empty() : managedTypes;
5655
}
5756

5857
@Override
59-
public void applyTo(@NonNull GenerationContext generationContext,
60-
@NonNull BeanRegistrationCode beanRegistrationCode) {
58+
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
6159

6260
List<Class<?>> types = getManagedTypes().toList();
6361

src/main/java/org/springframework/data/aot/Predicates.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
public abstract class Predicates {
3333

3434
public static final Predicate<Member> IS_ENUM_MEMBER = member -> member.getDeclaringClass().isEnum();
35-
public static final Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate");
35+
public static final Predicate<Member> IS_HIBERNATE_MEMBER = member -> member.getName().startsWith("$$_hibernate"); // this
36+
// should
37+
// go
38+
// into
39+
// JPA
3640
public static final Predicate<Member> IS_OBJECT_MEMBER = member -> Object.class.equals(member.getDeclaringClass());
3741
public static final Predicate<Member> IS_JAVA = member -> member.getDeclaringClass().getPackageName().startsWith("java.");
3842
public static final Predicate<Member> IS_NATIVE = member -> Modifier.isNative(member.getModifiers());

0 commit comments

Comments
 (0)