Mitigate some IntegrationMBeanExporter early BF access

* Fix typo in the `spring-integration.xsd`
This commit is contained in:
Artem Bilan
2024-08-13 14:15:42 -04:00
parent 4d787554b8
commit 9180240ae0
7 changed files with 23 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@ import org.springframework.util.StringUtils;
* @since 4.0
*/
@Configuration(proxyBeanMethods = false)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class IntegrationMBeanExportConfiguration implements ImportAware, EnvironmentAware, BeanFactoryAware {
/**

View File

@@ -18,19 +18,15 @@ package org.springframework.integration.jmx.config;
import java.util.UUID;
import javax.management.MBeanServerFactory;
import org.w3c.dom.Element;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.util.StringUtils;
/**
* Parser for the 'mbean-export' element of the integration JMX namespace.
@@ -57,7 +53,6 @@ public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
Object mbeanServer = getMBeanServer(element);
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-domain");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "object-name-static-properties");
@@ -65,18 +60,7 @@ public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
"componentNamePatterns");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "object-naming-strategy",
"namingStrategy");
builder.addPropertyValue("server", mbeanServer);
}
private Object getMBeanServer(Element element) {
String mbeanServer = element.getAttribute("server");
if (StringUtils.hasText(mbeanServer)) {
return new RuntimeBeanReference(mbeanServer);
}
else {
return MBeanServerFactory.createMBeanServer();
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "server");
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -208,8 +208,15 @@ public class IntegrationMBeanExporter extends MBeanExporter
this.attributeSource.setBeanFactory(beanFactory);
}
@Override
public void afterPropertiesSet() {
// Ignore 'super.afterPropertiesSet()' to avoid early 'BeanFactory' access
// and load 'MBeanServer' bypassing some 'BeanPostProcessor's
}
@Override
public void afterSingletonsInstantiated() {
super.afterPropertiesSet();
populateMessageHandlers();
populateMessageSources();
populateMessageChannels();

View File

@@ -9,7 +9,7 @@
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="mbs" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>
<bean id="mbs" class="org.springframework.jmx.support.MBeanServerFactoryBean" />
<si:channel id="testChannel"/>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,8 +24,10 @@ import javax.management.ObjectName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.context.IntegrationFlowContext;
@@ -98,6 +100,7 @@ public class DslMBeanTests {
public static class Config {
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static MBeanServerFactoryBean mbeanServer() {
return new MBeanServerFactoryBean();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,9 +27,11 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
@@ -107,10 +109,12 @@ public class EnableMBeanExportTests {
defaultDomain = "${managed.domain}",
managedComponents = {"input", "${managed.component}"})
@EnableIntegrationManagement(defaultLoggingEnabled = "false")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static class ContextConfiguration {
@Bean
public MBeanServerFactoryBean mbeanServer() {
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static MBeanServerFactoryBean mbeanServer() {
return new MBeanServerFactoryBean();
}