2016-09-29 102 views
0

春天開機不會自動裝配一個MBean的,我從另一個Web應用程序導出的:Spring Boot autowire 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; 
    } 
} 

我可以看到在甲骨文的Java任務控制,但其他的Spring應用程序啓動使用Bean是不能夠自動裝配豆。我這是我錯過了一個註釋。自動裝配我使用的豆:

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

任何想法?

+0

'@ Controller'和'@ Configuration'在同一個班?那可能嗎? – Pau

+0

@PauChorro我得到絕望,並嘗試了一切...... –

回答

3

在您想要從原始應用程序訪問管理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 
    // ... 
    // ... 
} 
+0

你是我的個人英雄:)它的工作!非常感謝你。 –