首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java spring引导中,我无法访问属性中的数据。

在java spring引导中,我无法访问属性中的数据。
EN

Stack Overflow用户
提问于 2019-11-14 18:22:22
回答 2查看 680关注 0票数 1

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

代码语言:javascript
复制
#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

我把它放进去:

代码语言:javascript
复制
src/test/resources

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

代码语言:javascript
复制
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;
}

我把它放进去:

代码语言:javascript
复制
src/main/java/mypackage/properties

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

代码语言:javascript
复制
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());
  }
}

我放进去的那一页:

代码语言:javascript
复制
src/main/java/mypackage/pages

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

代码语言:javascript
复制
<?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>

但它使返回值保持为空。

请帮助我,我已经读了很多教程,但仍然没有运气,除了我是初学者在春季启动。谢谢

EN

回答 2

Stack Overflow用户

发布于 2019-11-14 18:59:52

问题是命名属性文件的约定,因为您将它命名为application-QA1.properties,因此它成为配置文件特定的属性文件特殊配置文件属性,您可以通过在application.properties中激活配置文件(如这里 )来加载这些属性。

代码语言:javascript
复制
spring.profiles.active=QA1

或通过命令行

代码语言:javascript
复制
java -jar myproject.jar --spring.profiles.active=QA1

另一个信息,@Component注释中的值表示bean名称。

他的值可能表示一个逻辑组件名称的建议,在自动检测到组件的情况下,该名称将被转换为Spring。

票数 1
EN

Stack Overflow用户

发布于 2019-11-14 18:26:29

您指出属性文件的位置是:

代码语言:javascript
复制
<spring.config.location>classpath:/application-QA1.properties</spring.config.location>

但是你把属性文件放在一个包目录里..。

代码语言:javascript
复制
src/main/java/mypackage/properties

因此,当您构建项目时,属性文件不会以类路径的根结尾.它将在类路径的同一个包目录中结束。你自己看吧。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58863485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档