Java 在JMXJConsole中使用不带XML的Spring实现MBean
我想使用Spring注释创建Bean,而不需要任何XML配置。我有一些类似于Java 在JMXJConsole中使用不带XML的Spring实现MBean,java,spring,jmx,Java,Spring,Jmx,我想使用Spring注释创建Bean,而不需要任何XML配置。我有一些类似于的公共类WebAppInitializer实现WebApplicationInitializer,它加载所有应用程序上下文 我将bean作为一个类: @ManagedResource(objectName = "myBean.example:name=MonitoringService") @Component public class MonitoringService implements IMonitoringSe
的公共类WebAppInitializer实现WebApplicationInitializer
,它加载所有应用程序上下文
我将bean作为一个类:
@ManagedResource(objectName = "myBean.example:name=MonitoringService")
@Component
public class MonitoringService implements IMonitoringService {
public static final Logger LOG = LoggerFactory.getLogger(MonitoringService.class);
private boolean isDbServicesEnabled = true;
@ManagedAttribute(description = "DBServices configurator")
public boolean isDbServicesEnabled() {
return isDbServicesEnabled;
}
@ManagedAttribute(description = "DBServices configurator")
public void setDbServicesEnabled(boolean dbServicesEnabled) {
LOG.info("DBServices " + (isDbServicesEnabled ? "enabled" : "disabled"));
isDbServicesEnabled = dbServicesEnabled;
}
}
和简单的界面:
public interface IMonitoringService {
public boolean isDbServicesEnabled();
public void setDbServicesEnabled(boolean dbServicesEnabled);
}
我将它部署到GlassFish3服务器上,一切正常,但我看不到它是JConsole。
问题在哪里
我还使用SimonXbeanManager进行统计,它的包装器工作正常
我必须在MBeanServer中注册吗
这是SimonManager配置的一个类:
@Configuration
public class MonitoringConfiguration {
@Bean
public Manager simonManager() throws Exception {
ManagerFactoryBean managerFactoryBean = new ManagerFactoryBean();
Callback jmxRegisterCallback = new JmxRegisterCallback(mBeanServer(), "myBean.example");
managerFactoryBean.setCallbacks(Lists.newArrayList(jmxRegisterCallback));
return managerFactoryBean.getObject();
}
@Bean
public SimonManagerMXBeanWrapper simonManagerMXBean() throws Exception {
return new WebApplicationSimonManagerMXBeanWrapper(new SimonManagerMXBeanImpl(simonManager()));
}
@Bean
public MBeanServer mBeanServer() {
MBeanServerFactoryBean mBeanServerFactoryBean = new MBeanServerFactoryBean();
mBeanServerFactoryBean.setLocateExistingServerIfPossible(true);
mBeanServerFactoryBean.afterPropertiesSet();
return mBeanServerFactoryBean.getObject();
}
@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
return new AnnotationMBeanExporter();
}
@ManagedResource(objectName = "myBean.example:name=SimonManager")
private class WebApplicationSimonManagerMXBeanWrapper extends SimonManagerMXBeanWrapper {
public WebApplicationSimonManagerMXBeanWrapper(SimonManagerMXBean delegate) {
super(delegate);
}
}
以及加载应用程序上下文的类:
public class WebAppInitializer implements WebApplicationInitializer {
String[] locations = new String[] {
"myBean.example"
};
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(
RootContextConfig.class
);
rootContext.scan(locations);
rootContext.refresh();
container.addListener(new RequestContextListener());
container.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebAppConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet(
"dispatcher", new DispatcherServlet(dispatcherContext)
);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
createSimonWebConsole(container);
}
private void createSimonWebConsole(ServletContext container) {
ServletRegistration.Dynamic dn =
container.addServlet("simon-webconsole", new SimonConsoleServlet());
dn.setInitParameter("url-prefix", "/javasimon-console");
dn.addMapping("/javasimon-console/*");
}
}好的,我想,我必须添加
@ComponentScan
注释,并使用“myBean.example”
名称空间将其添加到类监视配置
。现在我可以在JConsole中配置我的服务了