0

我想使用 Spring 注释创建 Bean,而不需要任何 XML 配置。我有类似public class WebAppInitializer implements WebApplicationInitializer加载所有应用程序上下文的东西。

我有我的豆类:

@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);

}

我将它部署到 Glassfish 3 服务器,一切正常,但我看不到它 JConsole。问题出在哪里?

我还使用 SimonMXBeanManager 进行统计,它的包装器工作正常。

我必须在 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/*");
}

}

4

1 回答 1

0

好的,我想通了,我必须将@ComponentScan带有命名空间的注释添加"myBean.example"到 class MonitoringConfiguration。现在我可以在 JConsole 中配置我的服务。

于 2013-09-14T07:48:09.540 回答