Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JBoss MBean服务器上注册MBean-找不到JBoss MBean服务器_Java_Spring - Fatal编程技术网

Java 在JBoss MBean服务器上注册MBean-找不到JBoss MBean服务器

Java 在JBoss MBean服务器上注册MBean-找不到JBoss MBean服务器,java,spring,Java,Spring,我正在使用Spring公开MBean,并将其注册到JBoss MBean服务器。当战争被放到JBoss实例中时,这种方法很有效。但是,当运行单元测试时,它不起作用(这是有意义的,因为没有运行JBoss的实例) 以下是从spring配置中摘录的内容 <bean id="updateConfigMBean" class="mypackage.UpdateConfigMBean"/> <bean id="exporter" class="org.springframework.jm

我正在使用Spring公开MBean,并将其注册到JBoss MBean服务器。当战争被放到JBoss实例中时,这种方法很有效。但是,当运行单元测试时,它不起作用(这是有意义的,因为没有运行JBoss的实例) 以下是从spring配置中摘录的内容

<bean id="updateConfigMBean" class="mypackage.UpdateConfigMBean"/>

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="server">
    <bean class="org.jboss.mx.util.MBeanServerLocator" factory-method="locateJBoss"/>
</property>
<property name="beans">
<map>
<entry key="mypackage:name=configurationMBean" value-ref="updateConfigMBean"/>
</map>
</property>
</bean>

我想要的是一种优雅的方式来处理这个问题(不希望有两种spring配置(用于测试和部署),禁用spring配置验证测试不是一个选项


谢谢!

在这种情况下,您可能需要进行两次配置,一次用于测试,一次用于部署


这就是Maven的优势所在,因为您的部署配置和测试配置之间有一个明确的分离。如果您担心两个配置保持最新状态,那么您需要以这样一种方式构建配置:将所有公共位导入到其他配置中(我们就是这样做的).

我正在使用@Bean来解决这个问题。@Bean是为创建特定于环境的Bean而定制的

下面的逻辑基本上是,在开发(Tomcat)和测试(JUnit)中,使用MBeanServerFactoryBean,否则使用JBoss MBean服务器

  @Bean
  def mbeanServer: MBeanServer = {
    val server = if (environment == "development" || environment == "test") {
      val factory = new MBeanServerFactoryBean
      factory.setLocateExistingServerIfPossible(true)
      factory.setRegisterWithFactory(true)
      factory.afterPropertiesSet()
      log.info("using default MBeanServer")
      factory.getObject
    } else {
      val clazz = Class.forName("org.jboss.mx.util.MBeanServerLocator")
      val locateJboss = clazz.getMethod("locateJBoss", List.empty[Class[_]].toArray: _*)
      log.info("using JBoss MBeanServer")
      locateJboss.invoke(null, 
           List.empty[java.lang.Object].toArray: _*).asInstanceOf[MBeanServer]
    }
    log.info("mbeanServer: " + server)
    server
  }

在Spring 3.1中,您可以通过使用配置文件解决这个问题,并且仍然使用XML配置。但是上面的内容适用于Spring 3.0。

谢谢!我们使用Spring 3.1,所以我解决了配置文件的问题。