Skip to content

Commit 14931e1

Browse files
authored
Merge pull request #104 from kaiso/feature/#97
[feature] upgrade to spring 6 & spring-data 4
2 parents eb5c54f + 3f1f7c1 commit 14931e1

12 files changed

+601
-563
lines changed

pom.xml

+257-242
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.kaiso.relmongo.config;
22

3-
import io.github.kaiso.relmongo.events.processor.RelMongoProcessor;
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.lang.reflect.Method;
46

57
import org.springframework.beans.BeansException;
68
import org.springframework.beans.factory.BeanInitializationException;
@@ -9,69 +11,80 @@
911
import org.springframework.data.mongodb.core.MongoTemplate;
1012
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
1113

12-
import java.lang.reflect.Field;
14+
import io.github.kaiso.relmongo.events.processor.RelMongoProcessor;
1315

1416
public class RelMongoBeanPostProcessor implements BeanPostProcessor {
1517

16-
private final String mongoTemplateRef;
17-
18-
public RelMongoBeanPostProcessor(String mongoTemplateRef) {
19-
super();
20-
this.mongoTemplateRef = mongoTemplateRef;
21-
}
22-
23-
@Override
24-
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
25-
26-
if (bean instanceof MongoTemplate && beanName.equals(mongoTemplateRef)) {
27-
28-
/*
29-
* Enhancer enhancer = new Enhancer();
30-
* enhancer.setSuperclass(bean.getClass());
31-
* enhancer.setCallbacks(new MethodInterceptor[] { new
32-
* RelMongoTemplateInvocationHandler() });
33-
*
34-
* Object proxy = enhancer.create(new Class<?>[] { MongoDbFactory.class,
35-
* MongoConverter.class },
36-
* new Object[] { ((MongoTemplate) bean).getMongoDbFactory(), ((MongoTemplate)
37-
* bean).getConverter() });
38-
*
39-
* ((MongoTemplate) proxy).setApplicationContext(applicationContext);
40-
*
41-
* return proxy;
42-
*/
43-
44-
try {
45-
Field ep = MongoTemplate.class.getDeclaredField("eventPublisher");
46-
ep.setAccessible(true);
47-
ep.set(bean, new RelMongoEventPublisher((MongoTemplate) bean, (ApplicationEventPublisher) ep.get(bean)));
48-
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
49-
throw new BeanInitializationException("Fatal: failed to init the RelMongo Engine", e);
50-
}
51-
}
52-
53-
return bean;
54-
}
55-
56-
private static final class RelMongoEventPublisher implements ApplicationEventPublisher {
57-
58-
private final RelMongoProcessor relMongoProcessor;
59-
private final MongoTemplate mongoTemplate;
60-
private final ApplicationEventPublisher eventPublisher;
61-
62-
public RelMongoEventPublisher(MongoTemplate mongoTemplate, ApplicationEventPublisher eventPublisher) {
63-
super();
64-
this.relMongoProcessor = new RelMongoProcessor();
65-
this.mongoTemplate = mongoTemplate;
66-
this.eventPublisher = eventPublisher;
67-
}
68-
69-
@Override
70-
public void publishEvent(Object event) {
71-
relMongoProcessor.onApplicationEvent((MongoMappingEvent<?>) event, mongoTemplate);
72-
eventPublisher.publishEvent(event);
73-
}
74-
75-
}
18+
private final String mongoTemplateRef;
19+
20+
public RelMongoBeanPostProcessor(String mongoTemplateRef) {
21+
super();
22+
this.mongoTemplateRef = mongoTemplateRef;
23+
}
24+
25+
@Override
26+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
27+
28+
if (bean instanceof MongoTemplate && beanName.equals(mongoTemplateRef)) {
29+
30+
/*
31+
* Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(bean.getClass());
32+
* enhancer.setCallbacks(new MethodInterceptor[] { new
33+
* RelMongoTemplateInvocationHandler() });
34+
*
35+
* Object proxy = enhancer.create(new Class<?>[] { MongoDbFactory.class,
36+
* MongoConverter.class }, new Object[] { ((MongoTemplate)
37+
* bean).getMongoDbFactory(), ((MongoTemplate) bean).getConverter() });
38+
*
39+
* ((MongoTemplate) proxy).setApplicationContext(applicationContext);
40+
*
41+
* return proxy;
42+
*/
43+
44+
try {
45+
Field ep = MongoTemplate.class.getDeclaredField("eventPublisher");
46+
ep.setAccessible(true);
47+
48+
RelMongoEventPublisher eventPublisher = new RelMongoEventPublisher((MongoTemplate) bean,
49+
(ApplicationEventPublisher) ep.get(bean));
50+
ep.set(bean, eventPublisher);
51+
52+
Field edf = MongoTemplate.class.getDeclaredField("eventDelegate");
53+
edf.setAccessible(true);
54+
55+
Object ed = edf.get(bean);
56+
Method method = ed.getClass().getMethod("setPublisher", ApplicationEventPublisher.class);
57+
method.setAccessible(true);
58+
method.invoke(ed, eventPublisher);
59+
60+
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
61+
| InvocationTargetException | NoSuchMethodException e) {
62+
throw new BeanInitializationException("Fatal: failed to init the RelMongo Engine", e);
63+
}
64+
}
65+
66+
return bean;
67+
}
68+
69+
private static final class RelMongoEventPublisher implements ApplicationEventPublisher {
70+
71+
private final RelMongoProcessor relMongoProcessor;
72+
private final MongoTemplate mongoTemplate;
73+
private final ApplicationEventPublisher eventPublisher;
74+
75+
public RelMongoEventPublisher(MongoTemplate mongoTemplate, ApplicationEventPublisher eventPublisher) {
76+
super();
77+
this.relMongoProcessor = new RelMongoProcessor();
78+
this.mongoTemplate = mongoTemplate;
79+
this.eventPublisher = eventPublisher;
80+
}
81+
82+
@Override
83+
public void publishEvent(Object event) {
84+
relMongoProcessor.onApplicationEvent((MongoMappingEvent<?>) event, mongoTemplate);
85+
eventPublisher.publishEvent(event);
86+
}
87+
88+
}
7689

7790
}

src/main/java/io/github/kaiso/relmongo/config/RelMongoMappingContext.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.kaiso.relmongo.config;
22

3+
import java.util.AbstractMap;
4+
35
import org.springframework.data.mapping.context.MappingContext;
46
import org.springframework.data.mapping.model.FieldNamingStrategy;
57
import org.springframework.data.mapping.model.Property;
@@ -9,13 +11,12 @@
911
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty;
1012
import org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty;
1113
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
14+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
1215
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
1316
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
1417
import org.springframework.data.util.TypeInformation;
1518
import org.springframework.lang.Nullable;
1619

17-
import java.util.AbstractMap;
18-
1920
/**
2021
* Default implementation of a {@link MappingContext} for MongoDB using
2122
* {@link BasicMongoPersistentEntity} and
@@ -77,12 +78,12 @@ protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
7778
* org.springframework.data.mapping.SimpleTypeHolder)
7879
*/
7980
@Override
80-
public MongoPersistentProperty createPersistentProperty(Property property, BasicMongoPersistentEntity<?> owner,
81-
SimpleTypeHolder simpleTypeHolder) {
82-
return new CachingMongoPersistentProperty(property, owner, simpleTypeHolder, fieldNamingStrategy);
83-
}
81+
public MongoPersistentProperty createPersistentProperty(Property property, MongoPersistentEntity<?> owner,
82+
SimpleTypeHolder simpleTypeHolder) {
83+
return new CachingMongoPersistentProperty(property, owner, simpleTypeHolder, fieldNamingStrategy);
84+
}
8485

85-
/*
86+
/*
8687
* (non-Javadoc)
8788
*
8889
* @see

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertyConvertingCallback.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ public void doWith(Field field) throws IllegalAccessException {
5959
return;
6060
}
6161

62-
MappedByProcessor.processChild(source, null, field, ReflectionsUtil.getGenericType(field));
6362

6463
if (field.isAnnotationPresent(OneToMany.class)) {
6564
fillIdentifiers(field, field.getAnnotation(OneToMany.class).cascade());
6665
} else if (field.isAnnotationPresent(OneToOne.class)) {
6766
fillIdentifiers(field, field.getAnnotation(OneToOne.class).cascade());
6867
}
68+
69+
MappedByProcessor.processChild(source, null, field, ReflectionsUtil.getGenericType(field));
6970

7071
}
7172

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertyPostSavingCallback.java

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public PersistentPropertyPostSavingCallback(Object source, Class<?> sourceClass,
5252
public void apply() {
5353
ReflectionUtils.doWithFields(sourceClass, this);
5454
cache.stream().forEach(mongoOperations::save);
55+
cache.clear();
5556
}
5657

5758
public void doWith(Field field) throws IllegalAccessException {

0 commit comments

Comments
 (0)