Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBeanExporter.setExcludedBeans does not work properly on FactoryBeans [SPR-5926] #10595

Closed
spring-projects-issues opened this issue Jul 14, 2009 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Celal Ziftci opened SPR-5926 and commented

Inside the MBeanExporter.autodetect(AutodetectCallback) method, there is a check as follows:

<code>
String[] beanNames = this.beanFactory.getBeanNamesForType(Object.class, true, this.allowEagerInit);
...
if (!isExcluded(beanName)) {
...
}
</code>

Here, the bean names for the factory beans are returned with a ampersand in front (like &someBean), and when the MBeanExporter is used in Spring config with excludedBeans set, one has to use smth like this to get the factoryBean created beans excluded:

<code>
<bean class="org.springframework.jmx.MBeanExporter">
<property name="excludedBeans" value="&someBean" />
</bean>
</code>
So all in all, the framework should know how to handle factory beans and the beans created via them.


Referenced from: commits 566eeba

@spring-projects-issues
Copy link
Collaborator Author

Jeffrey Sinclair commented

I've investigated this a little further to provide a bit more context.

The problem here is with FactoryBeans and the way the MBeanExporter does auto-detection.

Take the following example (where CustomerFactory implements FactoryBean):

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
  <property name="excludedBeans" value="customer" />
</bean>
  <bean id="customer" class="spring.test.CustomerFactory" /> 

The MBeanExporter does the auto detection in afterPropertiesSet(). At this time the customer bean is not available, only the bean factory definition which Spring internally creates a bean name of &customer for. Once the customer bean is created, &customer will no longer exist. Hence if the MBeanExporter is before a factory bean, the exclude needs to prefix the bean id with a & for the exclude to work. If you reverse the ordering of the above beans, the excludedBeans property works as expected.

The auto dection logic needs to be done after all beans are created or somehow take the above situation into account.

@spring-projects-issues
Copy link
Collaborator Author

Dan Carwin commented

Customer requests confirmation that this fix is still on target for RC1.

Please confirm/correct.

Thanks, Dan Carwin.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This should be fixed in 3.0 RC1, with specified excluded names matched against unprefixed FactoryBean names as well (when applicable).

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Jeffrey Sinclair commented

Juergen,

I can confirm that the fix put in place does fix the original issue this JIRA was for, however there is still a fundamental ordering issue here causing an inconsistency.

I can do the following to ensure the customer bean is excluded thanks to this fix:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="excludedBeans" value="customer" />
</bean>
    
<bean id="customer" class="spring.test.CustomerFactory" /> 

However if I remove the excludedBeans property the customer is not included because the customer bean has yet to be created. I have to ensure my exporter is defined after the customer bean. Personally I don't like relying on this kind of ordering since it is very fragile, especially when dealing with multiple files.

Would you consider this a bug?

Would a BeanPostProcessor (perhaps one implementing InstantiationAware) be more suitable for this logic, i.e. also support lazily exporting beans as they are created?

Jeff

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Jeff, the ordering issue has been sorted out for 3.0 GA now.

Feel free to give the latest snapshot (503) a try: http://static.springsource.org/downloads/nightly/snapshot-download.php?project=SPR

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants