|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.metrics; |
18 | 18 |
|
19 | | -import java.io.Closeable; |
20 | | - |
21 | 19 | import io.micrometer.core.instrument.MeterRegistry; |
| 20 | +import io.micrometer.core.instrument.binder.MeterBinder; |
22 | 21 | import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; |
23 | 22 | import io.micrometer.core.instrument.binder.jvm.JvmCompilationMetrics; |
24 | 23 | import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; |
|
32 | 31 | import org.springframework.aot.hint.RuntimeHintsRegistrar; |
33 | 32 | import org.springframework.beans.BeanUtils; |
34 | 33 | import org.springframework.beans.factory.BeanClassLoaderAware; |
35 | | -import org.springframework.beans.factory.DisposableBean; |
36 | | -import org.springframework.beans.factory.FactoryBean; |
37 | 34 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
38 | 35 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
39 | 36 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
|
53 | 50 | @AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class }) |
54 | 51 | @ConditionalOnClass(MeterRegistry.class) |
55 | 52 | @ConditionalOnBean(MeterRegistry.class) |
56 | | -public class JvmMetricsAutoConfiguration { |
| 53 | +public class JvmMetricsAutoConfiguration implements BeanClassLoaderAware { |
57 | 54 |
|
58 | 55 | private static final String VIRTUAL_THREAD_METRICS_CLASS = "io.micrometer.java21.instrument.binder.jdk.VirtualThreadMetrics"; |
59 | 56 |
|
| 57 | + private ClassLoader beanClassLoader; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 61 | + this.beanClassLoader = classLoader; |
| 62 | + } |
| 63 | + |
60 | 64 | @Bean |
61 | 65 | @ConditionalOnMissingBean |
62 | 66 | public JvmGcMetrics jvmGcMetrics() { |
@@ -103,47 +107,9 @@ public JvmCompilationMetrics jvmCompilationMetrics() { |
103 | 107 | @ConditionalOnClass(name = VIRTUAL_THREAD_METRICS_CLASS) |
104 | 108 | @ConditionalOnMissingBean(type = VIRTUAL_THREAD_METRICS_CLASS) |
105 | 109 | @ImportRuntimeHints(VirtualThreadMetricsRuntimeHintsRegistrar.class) |
106 | | - VirtualThreadMetricsFactoryBean virtualThreadMetrics() { |
107 | | - return new VirtualThreadMetricsFactoryBean(); |
108 | | - } |
109 | | - |
110 | | - static final class VirtualThreadMetricsFactoryBean |
111 | | - implements FactoryBean<Object>, BeanClassLoaderAware, DisposableBean { |
112 | | - |
113 | | - private ClassLoader classLoader; |
114 | | - |
115 | | - private Class<?> instanceType; |
116 | | - |
117 | | - private Object instance; |
118 | | - |
119 | | - @Override |
120 | | - public void setBeanClassLoader(ClassLoader classLoader) { |
121 | | - this.classLoader = classLoader; |
122 | | - } |
123 | | - |
124 | | - @Override |
125 | | - public Object getObject() { |
126 | | - if (this.instance == null) { |
127 | | - this.instance = BeanUtils.instantiateClass(getObjectType()); |
128 | | - } |
129 | | - return this.instance; |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - public Class<?> getObjectType() { |
134 | | - if (this.instanceType == null) { |
135 | | - this.instanceType = ClassUtils.resolveClassName(VIRTUAL_THREAD_METRICS_CLASS, this.classLoader); |
136 | | - } |
137 | | - return this.instanceType; |
138 | | - } |
139 | | - |
140 | | - @Override |
141 | | - public void destroy() throws Exception { |
142 | | - if (this.instance instanceof Closeable closeable) { |
143 | | - closeable.close(); |
144 | | - } |
145 | | - } |
146 | | - |
| 110 | + MeterBinder virtualThreadMetrics() { |
| 111 | + Class<?> clazz = ClassUtils.resolveClassName(VIRTUAL_THREAD_METRICS_CLASS, this.beanClassLoader); |
| 112 | + return (MeterBinder) BeanUtils.instantiateClass(clazz); |
147 | 113 | } |
148 | 114 |
|
149 | 115 | static final class VirtualThreadMetricsRuntimeHintsRegistrar implements RuntimeHintsRegistrar { |
|
0 commit comments