|
31 | 31 | import org.apache.tomcat.jdbc.pool.jmx.ConnectionPool;
|
32 | 32 | import org.junit.Test;
|
33 | 33 |
|
| 34 | +import org.springframework.aop.framework.ProxyFactory; |
| 35 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
34 | 36 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
35 | 37 | import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
36 | 38 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
| 39 | +import org.springframework.context.annotation.Bean; |
| 40 | +import org.springframework.context.annotation.Configuration; |
37 | 41 |
|
38 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
39 | 43 |
|
@@ -101,6 +105,24 @@ public void hikariAutoConfiguredUsesJmsFlag() {
|
101 | 105 | });
|
102 | 106 | }
|
103 | 107 |
|
| 108 | + @Test |
| 109 | + public void hikariProxiedCanUseRegisterMBeans() { |
| 110 | + String poolName = UUID.randomUUID().toString(); |
| 111 | + this.contextRunner.withUserConfiguration(DataSourceProxyConfiguration.class) |
| 112 | + .withPropertyValues( |
| 113 | + "spring.datasource.type=" + HikariDataSource.class.getName(), |
| 114 | + "spring.datasource.name=" + poolName, |
| 115 | + "spring.datasource.hikari.register-mbeans=true") |
| 116 | + .run((context) -> { |
| 117 | + assertThat(context).hasSingleBean(javax.sql.DataSource.class); |
| 118 | + HikariDataSource hikariDataSource = context.getBean( |
| 119 | + javax.sql.DataSource.class).unwrap(HikariDataSource.class); |
| 120 | + assertThat(hikariDataSource.isRegisterMbeans()).isTrue(); |
| 121 | + MBeanServer mBeanServer = context.getBean(MBeanServer.class); |
| 122 | + validateHikariMBeansRegistration(mBeanServer, poolName, true); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
104 | 126 | private void validateHikariMBeansRegistration(MBeanServer mBeanServer,
|
105 | 127 | String poolName, boolean expected) throws MalformedObjectNameException {
|
106 | 128 | assertThat(mBeanServer.isRegistered(
|
@@ -130,4 +152,31 @@ public void tomcatAutoConfiguredCanExposeMBeanPool() {
|
130 | 152 | });
|
131 | 153 | }
|
132 | 154 |
|
| 155 | + @Configuration |
| 156 | + static class DataSourceProxyConfiguration { |
| 157 | + |
| 158 | + @Bean |
| 159 | + public static DataSourceBeanPostProcessor dataSourceBeanPostProcessor() { |
| 160 | + return new DataSourceBeanPostProcessor(); |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + private static class DataSourceBeanPostProcessor implements BeanPostProcessor { |
| 167 | + |
| 168 | + @Override |
| 169 | + public Object postProcessAfterInitialization(Object bean, String beanName) { |
| 170 | + if (bean instanceof javax.sql.DataSource) { |
| 171 | + return wrap((javax.sql.DataSource) bean); |
| 172 | + } |
| 173 | + return bean; |
| 174 | + } |
| 175 | + |
| 176 | + private static javax.sql.DataSource wrap(javax.sql.DataSource dataSource) { |
| 177 | + return (javax.sql.DataSource) new ProxyFactory(dataSource).getProxy(); |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
133 | 182 | }
|
0 commit comments