代码之家  ›  专栏  ›  技术社区  ›  Andriy Volkov

在Spring.config中,我可以定义string类型的对象吗?

  •  0
  • Andriy Volkov  · 技术社区  · 17 年前

    我可以这样做吗:

      <object id="mydb" type="string">
        <value>"blah"</value> <-- note that <value> tag does not really exist
      </object>
    

      <object id="Someobject" type="Sometype">
        <property name="ConnectionString" ref="mydb"/>
      </object>
    

    编辑:这是我正在寻找的SpringFramework.NET解决方案。看起来那里也存在PropertyPlaceHolderConfigure。谢谢大家。

    3 回复  |  直到 14 年前
        1
  •  2
  •   flicken    17 年前

    使用Spring内置的 PropertyPlaceholdConfigurer

    <bean id="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
       <property name="location" value="classpath*:/application.properties"/>
    </bean>
    
     <bean id="Someobject" class="somepackage.Sometype">
       <property name="connectionString" value="${mydb}"/>
     </bean>
    

    SYSTEM_PROPERTIES_MODE_OVERRIDE 允许通过命令行重写属性。

        2
  •  1
  •   mP.    17 年前

    使用占位符,例如 并在属性文件和后处理器中定义键/值。谷歌 spring后置处理器占位符 ...

        3
  •  0
  •   duffymo    17 年前

    有时,人们将数据库连接字符串外部化到.properties文件,并以这种方式获取它们。我认为这比你的建议更有意义。