在 Spring 中设置属性

发布于 2024-10-27 08:54:31 字数 2784 浏览 5 评论 0原文

有没有办法改变文件的属性?我正在尝试使用 Spring 和 Jetty 并行运行 selenium 测试,因此我正在尝试配置数据库的 url、jettyserver 的端口和 selenium 服务器的端口。这样我就能够初始化两个或更多可以运行测试的服务器。

我的 server.properties 文件包含以下内容:

jdbc.url=jdbc:hsqldb:hsql://localhost/bibliothouris_scenario
jetty.port=8081
seleniumServer.port=4444

我可以使用 PropertyPlaceholderConfigurer 读取这些属性,并且我需要灵活的数据库 URL、jettyport 和 seleniumserver 端口。

我已经这样声明它们:

在我的 applicationContext.xml 中:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:server.properties</value>
    </property>
</bean>

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

在 serverContext.xml 文件中:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:server.properties</value>
    </property>
</bean>

<bean class="com.~companyName~.bibliothouris.jetty.JettyServer" init-method="start" destroy-method="stop">
    <constructor-arg value="${jetty.port}" />
    <constructor-arg ref="dataSource" />
</bean>

<bean class="org.openqa.selenium.server.SeleniumServer" init-method="start" destroy-method="stop">
    <constructor-arg>
        <bean class="org.openqa.selenium.server.RemoteControlConfiguration">
            <property name="port" value="${seleniumServer.port}" />
            <property name="singleWindow" value="true" />
            <property name="timeoutInSeconds" value="10" />
        </bean>
    </constructor-arg>
</bean>

<bean class="com.thoughtworks.selenium.DefaultSelenium" init-method="start" destroy-method="stop" lazy-init="true">
    <constructor-arg>
        <bean class="com.thoughtworks.selenium.HttpCommandProcessor">
            <constructor-arg value="localhost" />
            <constructor-arg value="${seleniumServer.port}" />
            <constructor-arg value="*firefox c:/~companyname~/firefox/firefox.exe" />
            <constructor-arg value="http://localhost:${jetty.port}" />
        </bean>
    </constructor-arg>
</bean>

当我更改 server.properties 中的数据时,selenium 测试在具有正确端口的正确服务器上运行,没有失败。

所以现在我正在寻找一种方法来更改 server.properties 文件中的属性。

提前致以亲切的问候和感谢

Is there a way to change the properties of a file? I'm trying to run selenium tests in parallel, with Spring and Jetty, so I'm trying to configure the url of the database, the port of the jettyserver and the port of the selenium server. So that I'm able to initialize two or more servers where the tests can run on.

My server.properties file contains this:

jdbc.url=jdbc:hsqldb:hsql://localhost/bibliothouris_scenario
jetty.port=8081
seleniumServer.port=4444

I can read those properties with a PropertyPlaceholderConfigurer, and I need the database URL, jettyport and seleniumserver port to be flexible.

I have declared them like this:

In my applicationContext.xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:server.properties</value>
    </property>
</bean>

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

In the serverContext.xml file:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:server.properties</value>
    </property>
</bean>

<bean class="com.~companyName~.bibliothouris.jetty.JettyServer" init-method="start" destroy-method="stop">
    <constructor-arg value="${jetty.port}" />
    <constructor-arg ref="dataSource" />
</bean>

<bean class="org.openqa.selenium.server.SeleniumServer" init-method="start" destroy-method="stop">
    <constructor-arg>
        <bean class="org.openqa.selenium.server.RemoteControlConfiguration">
            <property name="port" value="${seleniumServer.port}" />
            <property name="singleWindow" value="true" />
            <property name="timeoutInSeconds" value="10" />
        </bean>
    </constructor-arg>
</bean>

<bean class="com.thoughtworks.selenium.DefaultSelenium" init-method="start" destroy-method="stop" lazy-init="true">
    <constructor-arg>
        <bean class="com.thoughtworks.selenium.HttpCommandProcessor">
            <constructor-arg value="localhost" />
            <constructor-arg value="${seleniumServer.port}" />
            <constructor-arg value="*firefox c:/~companyname~/firefox/firefox.exe" />
            <constructor-arg value="http://localhost:${jetty.port}" />
        </bean>
    </constructor-arg>
</bean>

When I change the data in server.properties the selenium tests run on the right servers with the right ports, without failures.

So now I'm looking for a method to change the properties in the server.properties file.

Kind regards and thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-11-03 08:54:31

我通过在构建过程中设置一个标志(我使用 Maven)来解决这个问题,该标志选择要包含在最终战争中的属性文件。通过这种方式,您可以包含具有不同属性的不同工件(不同的属性文件),而不必扰乱 Spring 的低级属性支持。

如果您确实需要仅在 Spring 中执行此操作,我建议您使用基于 Java 的配置,您可以在代码中而不是 XML 中获取和设置属性。

I solved this by having a flag in my build process (I'm using Maven) that chose which property file to include in the final war. This way you can include different artifacts (different property files) with different properties without having to mess with the low level property support of Spring.

If you do need to do this is Spring only, I would recommend going for a Java based configuration, where you can get and set the properties by in code not in XML.

捶死心动 2024-11-03 08:54:31

有没有办法改变
文件的属性?

不可以,但是您可以通过以下方式解决此问题。

  • 将属性拆分为 jdbc.properties(对于 applicationContext.xml)和 test.properties(对于 serverContext.xml)
  • 通过 src/test/resources 资源覆盖 server.properties
  • 除 server.properties 之外还使用系统属性(使用 PropertyPlaceholderConfigurer.setSystemPropertiesMode 进行这)

Is there a way to change the
properties of a file?

No, but you could solve this in the following ways.

  • Split the properties into jdbc.properties (for applicationContext.xml) and test.properties (for serverContext.xml)
  • override server.properties via a src/test/resources resource
  • use system properties in addition to server.properties (use PropertyPlaceholderConfigurer.setSystemPropertiesMode for this)
迷爱 2024-11-03 08:54:31

感谢您的帮助,没有您的信息,我找不到自己的解决方案。就是这样:

try {
        Properties props = new Properties();
        FileInputStream fileInputStream = new FileInputStream(
            "C:\\~CompanyName~\\workspace\\bibliothouris\\infrastructure\\src\\main\\resources\\server.properties");
        props.load(fileInputStream);
        fileInputStream.close();
        props.setProperty("seleniumServer.port", "4445");

        FileOutputStream fileOutputStream = new FileOutputStream(
            "C:\\~CompanyName~\\workspace\\bibliothouris\\infrastructure\\src\\main\\resources\\server.properties");
        props.store(fileOutputStream, "");
        fileOutputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

我已经在测试类中编写了这段代码,现在我必须创建它的一个方法,该方法需要一些参数(URL、jettyport 和 seleniumport)。我必须将路径更改为相对路径。

感谢您的帮助!

Thanks for the help guys, without your info, I couldn't find my own solution. Here it is:

try {
        Properties props = new Properties();
        FileInputStream fileInputStream = new FileInputStream(
            "C:\\~CompanyName~\\workspace\\bibliothouris\\infrastructure\\src\\main\\resources\\server.properties");
        props.load(fileInputStream);
        fileInputStream.close();
        props.setProperty("seleniumServer.port", "4445");

        FileOutputStream fileOutputStream = new FileOutputStream(
            "C:\\~CompanyName~\\workspace\\bibliothouris\\infrastructure\\src\\main\\resources\\server.properties");
        props.store(fileOutputStream, "");
        fileOutputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

I've wrote this piece of code in a testclass, now I have to create a method of it, which takes a few arguments (the URL, jettyport and seleniumport). And I have to change the path to a relative one.

Thanks for the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文