-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
SharedApplicationContextHolder.java
80 lines (70 loc) · 3.49 KB
/
SharedApplicationContextHolder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package run.halo.app.plugin;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
import org.springframework.stereotype.Component;
import run.halo.app.core.extension.service.AttachmentService;
import run.halo.app.extension.DefaultSchemeManager;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.BackupRootGetter;
import run.halo.app.infra.ExternalUrlSupplier;
/**
* <p>This {@link SharedApplicationContextHolder} class is used to hold a singleton instance of
* {@link SharedApplicationContext}.</p>
* <p>If sharedApplicationContext cache is null when calling the {@link #getInstance()} method,
* then it will call {@link #createSharedApplicationContext()} to create and cache it. Otherwise,
* it will be obtained directly.</p>
* <p>It is thread safe.</p>
*
* @author guqing
* @since 2.0.0
*/
@Component
public class SharedApplicationContextHolder {
private final ApplicationContext rootApplicationContext;
private volatile SharedApplicationContext sharedApplicationContext;
public SharedApplicationContextHolder(ApplicationContext applicationContext) {
this.rootApplicationContext = applicationContext;
}
/**
* Get singleton instance of {@link SharedApplicationContext}.
*
* @return a singleton instance of {@link SharedApplicationContext}.
*/
public SharedApplicationContext getInstance() {
if (this.sharedApplicationContext == null) {
synchronized (SharedApplicationContextHolder.class) {
if (this.sharedApplicationContext == null) {
this.sharedApplicationContext = createSharedApplicationContext();
}
}
}
return this.sharedApplicationContext;
}
SharedApplicationContext createSharedApplicationContext() {
// TODO Optimize creation timing
SharedApplicationContext sharedApplicationContext = new SharedApplicationContext();
sharedApplicationContext.refresh();
DefaultListableBeanFactory beanFactory =
(DefaultListableBeanFactory) sharedApplicationContext.getBeanFactory();
// register shared object here
var extensionClient = rootApplicationContext.getBean(ExtensionClient.class);
var reactiveExtensionClient = rootApplicationContext.getBean(ReactiveExtensionClient.class);
beanFactory.registerSingleton("extensionClient", extensionClient);
beanFactory.registerSingleton("reactiveExtensionClient", reactiveExtensionClient);
DefaultSchemeManager defaultSchemeManager =
rootApplicationContext.getBean(DefaultSchemeManager.class);
beanFactory.registerSingleton("schemeManager", defaultSchemeManager);
beanFactory.registerSingleton("externalUrlSupplier",
rootApplicationContext.getBean(ExternalUrlSupplier.class));
beanFactory.registerSingleton("serverSecurityContextRepository",
rootApplicationContext.getBean(ServerSecurityContextRepository.class));
beanFactory.registerSingleton("attachmentService",
rootApplicationContext.getBean(AttachmentService.class));
beanFactory.registerSingleton("backupRootGetter",
rootApplicationContext.getBean(BackupRootGetter.class));
// TODO add more shared instance here
return sharedApplicationContext;
}
}