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

FactoryBeans returning a @ManagedResource annotated subclass are not auto detected by MBeanExporter [SPR-6540] #11206

Closed
spring-projects-issues opened this issue Dec 9, 2009 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 9, 2009

Jeffrey Sinclair opened SPR-6540 and commented

Take the following application context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <bean id="customer" class="spring.test.CustomerFactory">
    <property name="name" value="Jeff Sinclair" />
  </bean>

  <bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
    <property name="autodetect" value="true" />
  </bean>

</beans>

with CustomerFactory being defined as:

public class CustomerFactory implements FactoryBean<Customer> {

    private String name;

    @Override
    public Customer getObject() throws Exception {
        return new DefaultCustomer(this.name);
    }

    @Override
    public Class<? extends Customer> getObjectType() {
        return Customer.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setName(String name) {
        this.name = name;
    }
}

with the Customer interface being defined as:

public interface Customer {
    public String getName();   
}

with the DefaultCustomer impl being defined as:

@ManagedResource
public class DefaultCustomer implements Customer {

    private String name;

    public DefaultCustomer(String name) {
        this.name = name;
    }

    @Override
    @ManagedAttribute
    public String getName() {
        return name;
    }
}

Since the autodetect logic only sees the Customer class (which is not annotated with @ManagedResource) it does not export the DefaultCustomer instance (which is annotated with @ManagedResource).

There is also the problem that even if the Customer was annotated with @ManagedResource the bean definition must be before the MBeanExporter's bean definition for this to work.

I added a comment to the following JIRA #10595 describing this issue providing the suggestion that a BeanPostProcessor (perhaps implementing InstantiationAware) would be more suitable for the auto detection logic.


Affects: 3.0 RC3

1 votes, 2 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Dec 13, 2009

Juergen Hoeller commented

I'm afraid you'll have to expose the full DefaultCustomer.class from your getObjectType() method there. We generally recommend implementing that method to return the most specific type possible, even if the FactoryBean is generically typed to a plain interface type such as FactoryBean<Customer>. Otherwise you're 'hiding' the specific type and hence prevent introspection of the implementation type; this can be used deliberately but in your case it's not intentional I suppose.

The ordering issue has been sorted out for 3.0 GA now, as commented on #10595. Thanks for raising that one again - it was nearly getting lost.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
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) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants