java - 我无法从 java spring boot 中的属性访问数据

标签 java maven spring-boot selenium cucumber

我有名为 application-QA1.properties 的属性

#Endpoint
web.loginPage=https://www.phptravels.net/

#login data
common.data.username=tes
common.data.password=tes

#Default Param
param.storeId=10001
param.channelId=tes-web
param.clientId=10001
param.requestId=RANDOM
param.businessChannel=web
param.requestParams={}
param.test=true

我把它放进去:

src/test/resources

我创建类作为名为 WebProperties.java 的属性的实例

package com.project.automation.ui.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component("com.project.automation.ui.properties.WebProperties")
@ConfigurationProperties(prefix = "web")
public class WebProperties {
    private String loginPage;
}

我把它放进去:

src/main/java/mypackage/properties

我想访问 LoginPage.java 上的类变量

package com.project.automation.ui.pages;

import com.project.automation.ui.models.xpath.CommonXpathModel;
import com.project.automation.ui.properties.CommonProperties;
import com.project.automation.ui.properties.WebProperties;
import lombok.Data;
import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.By;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Data
@Component("com.project.automation.ui.pages.LoginPage")
public class LoginPage extends PageObject {

  @Autowired
  CommonPage commonPage;

  @Autowired
  CommonXpathModel commonXpathModel;

  @Autowired
  WebProperties webProperties;

  @Autowired
  CommonProperties commonProperties;

  public void openPage(){
    //on this code i want to get that properties value through its class instance
    getDriver().get(webProperties.getLoginPage());
  }
}

我放入的页面:

src/main/java/mypackage/pages

这是我的 pom.xml,位于我的项目根目录中。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.project.automation.ui</groupId>
    <artifactId>wina</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>1.9.45</serenity.version>
        <maven.failsafe.plugin>2.22.0</maven.failsafe.plugin>
        <selenium.version>3.14.0</selenium.version>
        <buildDirectory>${project.basedir}/target</buildDirectory>
        <serenity.testlink.integration.version>3.4.5</serenity.testlink.integration.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.lambdaj</groupId>
            <artifactId>lambdaj</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>1.9.45</version>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-spring</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-rest-assured</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <directory>${buildDirectory}</directory>
        <testOutputDirectory>${buildDirectory}/test-classes</testOutputDirectory>
        <outputDirectory>${buildDirectory}/classes</outputDirectory>
        <resources>
            <resource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.failsafe.plugin}</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includes>
                        <include>**/CucumberRunner.java</include>
                    </includes>
                    <systemPropertyVariables>
                        <cucumber.options>--junit,--step-notifications</cucumber.options>
                        <spring.config.location>classpath:/application-QA1.properties</spring.config.location>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

但它一直返回null

请帮助我,我已经阅读了很多教程,但仍然没有运气,而且我是 Spring Boot 的初学者。谢谢

最佳答案

问题是属性文件的命名约定,由于您将其命名为application-QA1.properties,它成为配置文件特定的属性文件 Profile-specific properties您可以通过在 application.properties 中激活配置文件来加载这些属性,例如 here

spring.profiles.active=QA1

或者通过命令行

java -jar myproject.jar --spring.profiles.active=QA1

还有一个信息,@Component注解中的值表示bean名称

he value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.

关于java - 我无法从 java spring boot 中的属性访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58863485/

相关文章:

java - 是否可以获取打包项目时使用的maven命令?

java - spring boot,jetty,liquibase,jar,找不到 liquibase.logging.Logger 的实现

spring - 有没有什么大型的 spring-boot 开源项目?

spring-boot - 将 Spring Boot 配置文件添加到 Sleuth/Zipkin 日志

java - 推断泛型类中的参数类型,这些参数类型是所提供参数的嵌套泛型类型

java - 如何在 Android 上顺序运行 java 线程

java - 如何在 Mac 上从 ant 启动 shell 脚本?

java - iText 版本 4.2.1 在 Maven 中央存储库中重定向

java - Apache Tomcat 为 Web 应用程序返回 404 错误

java - 由 : java. lang.ClassNotFoundException : com. sun.xml.bind.v2.model.annotation.AnnotationReader 引起