2010-05-05 100 views
2

當我編寫Java EE應用程序時,我使用JBoss Datasources來控制部署所使用的數據庫。例如。 dev版本會使用一次性的hibernate db,ref和ops會使用穩定的MySQL部署。我還使用MBeans來配置各種其他服務和規則。Spring部署級配置

現在我正在使用Spring,我想要相同的功能 - 部署相同的代碼,但具有不同的配置。最重要的是,我還希望單元測試仍然使用存根服務運行。我的問題是這樣的 - 在JBoss中,有沒有辦法在WAR/EAR以外的文件中注入配置,並將這些文件包含在測試資源中。

+0

Spring可以使用與以前相同的JavaEE數據源,爲什麼不繼續這樣做呢?同樣,Spring有很好的MBean支持。 – skaffman 2010-05-05 18:41:01

回答

2

通過將名爲xxx-service.xml的文件放入jboss的deploy目錄中,可以將對象添加到JNDI上下文中。應用程序可以通過JNDI查找值。在下面的示例中,在java:/ modes/deployment中添加了字符串「development」。要在您的單元測試中使用JNDI,請使用org.springframework.mock.jndi包。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE server PUBLIC "-//JBoss//DTD MBean Service 4.0//EN" 
      "http://www.jboss.org/j2ee/dtd/jboss-service_4_0.dtd"> 
<server> 
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" 
     name="c3po.naming:service=jndi-bindings"> 
     <attribute name="BindingsConfig" serialDataType="jbxb"> 
     <jndi:bindings 
      xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:jndi="urn:jboss:jndi-binding-service:1.0" 
      xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd" 
      > 

      <jndi:binding name="java:/modes/deployment"> 
       <jndi:value type="java.lang.String">development</jndi:value> 
      </jndi:binding> 

      <jndi:binding name="java:/sites/abc"> 
       <jndi:value type="java.lang.String">dev.site.example.com</jndi:value> 
      </jndi:binding> 

<!-- Examples: 

      <jndi:binding name="urls/jboss-home"> 
       <jndi:value type="java.net.URL">http://www.jboss.org</jndi:value> 
      </jndi:binding> 

      <jndi:binding name="hosts/localhost"> 
       <jndi:value editor="org.jboss.util.propertyeditor.InetAddressEditor"> 
        127.0.0.1 
       </jndi:value> 
      </jndi:binding> 

      <jndi:binding name="maps/testProps"> 
       <java:properties xmlns:java="urn:jboss:java-properties" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
        xs:schemaLocation="urn:jboss:java-properties resource:java-properties_1_0.xsd"> 
        <java:property> 
        <java:key>key1</java:key> 
        <java:value>value1</java:value> 
        </java:property> 
        <java:property> 
        <java:key>key2</java:key> 
        <java:value>value2</java:value> 
        </java:property> 
       </java:properties>    
      </jndi:binding> 
--> 

     </jndi:bindings> 
     </attribute> 
     <depends>jboss:service=Naming</depends> 
    </mbean> 

</server> 
+0

輝煌。正是我需要的。 – 2010-05-11 11:05:23