【问题标题】:Spring Boot autowire MBean?Spring Boot自动装配MBean?
【发布时间】:2016-09-29 13:25:02
【问题描述】:

Spring Boot 不会自动装配我从另一个 Web 应用程序导出的 MBean:

@Component
@Service
@ManagedResource(objectName = IHiveService.MBEAN_NAME)
public class HiveService implements IHiveService {
    @Autowired(required = true)
    CategoryRepository categoryRepository;

    @Override
    @ManagedOperation
    public String echo(String input) {
        return "you said " + input;
    }
}

我可以在 Oracle Java Mission Control 中看到和使用 Bean,但其他 Spring Boot 应用程序无法自动装配 bean。我错过了一个注释。要自动装配我使用的 bean:

@Controller
@Configuration 
@EnableMBeanExport
public class GathererControls {
   @Autowired
   IHiveService hiveService; // <-- this should be auto wired

有什么想法吗?

【问题讨论】:

  • @Controller@Configuration 在同一个班级??这可能吗?
  • @PauChorro 我绝望了,尝试了一切......

标签: spring-boot jmx autowired spring-jmx


【解决方案1】:

您不需要在要从原始应用程序访问管理 bean 的应用程序中使用 @EnableMBeanExport 注释。

您需要的是与 JMX 注册表的连接,以访问导出的(由第一个应用程序)管理对象。

@Configuration
public class MyConfiguration {

  @Bean
  public MBeanProxyFactoryBean hiveServiceFactory() {
    MBeanProxyFactoryBean proxyFactory = new MBeanProxyFactoryBean();
    proxyFactory.setObjectName(IHiveService.MBEAN_NAME);
    proxyFactory.setProxyInterface(IHiveService.class);
    proxyFactory.afterPropertiesSet();
    return proxyFactory;
  }

  @Bean 
  public IHiveService hiveService(MBeanProxyFactoryBean hiveServiceFactory) {
    return (IHiveService) hiveServiceFactory.getObject();
  }
}

现在在您的控制器中:

@Controller
public class GathererControls {
   @Autowired
   IHiveService hiveService; // <-- will be autowired
   // ...
   // ...
}

【讨论】:

  • 你是我的个人英雄 :) 它正在工作!非常感谢。
猜你喜欢
  • 2017-09-07
  • 2019-05-03
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多