gpt4 book ai didi

java - 使用 Spring 加载显示 NPE 的 java 属性文件

转载 作者:行者123 更新时间:2023-12-01 09:45:11 27 4
gpt4 key购买 nike

我是 Spring 的新手,我正在尝试使用 Spring 框架加载属性文件,我能够成功地从 junit 测试加载所有属性,但是当我尝试将单元测试实现为函数时,它会抛出 NPE-

我的 junit 类(按预期工作)

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(profiles = "test")
@ContextConfiguration("classpath:spring/xml-config-context.xml")
public class GlueTestPropertiesTest2 extends TestCase {

@Autowired
GenericEnv env;

@Autowired
WebPropertiesLoader wpl;

@Test
public void testAppProperties() {

System.out.println("Running MiniConfigSpringPropertiesTest ...");

System.out.println("Environment : " + env.toString());

System.out.println("Database Properties: " + wpl.toString());
}

}

我的实现类(正在展示 NPE):

@ActiveProfiles(profiles = "test")
@ContextConfiguration("classpath:spring/xml-config-context.xml")
public class GlueTestProperties {

@Autowired
GenericEnv env;

@Autowired
WebPropertiesLoader wpl;

public static void main(String[] args) {
GlueTestProperties gp = new GlueTestProperties();
gp.callme();


}


private void callme(){

System.out.println("Running ConfigSpringPropertiesTest ...");

System.out.println("Environment : " + env.toString());

System.out.println("Database Properties: " + wpl.toString());
}
}

WebPropertiesLoader bean:

@Component
public class WebPropertiesLoader {

@Value("${bank.ease.login.url}")
public String easeLoginUrl;

@Value("${bank.browser.name}")
public String browserName;

@Value("${bank.browser.version}")
public String browserVersion;

@Value("${webdriver.chrome.driver}")
public String chromeDriver;

@Value("${webdriver.ie.driver}")
public String ieDriver;

@Value("${bank.web.feature.location}")
public String webFeatureLocation;

@Value("${bank.web.test.location}")
public String webTestLocation;

@Value("${bank.event.log}")
public String eventLog;

@Value("${bank.epoxy.backend}")
public String epoxyBackend;

@Value("${bank.epoxy.host}")
public String epoxyHost;

@Value("${bank.epoxy.port}")
public String epoxyPort;

@Value("${bank.epoxy.debug}")
public String epoxyDebug;

@Value("${bank.epoxy.implicitWait}")
public String epoxyImplicitWait;

@Value("${bank.epoxy.timeout}")
public String epoxyTimeOut;

@Value("${bank.epoxy.default.url}")
public String epoxyDefaultURL;

@Value("${bank.sassy.url}")
public String sassyUrl;

@Value("${bank.transite.url}")
public String transiteUrl;

@Value("${bank.transite.login.url}")
public String transiteLoginUrl;


public String getBrowserName() {
return browserName;
}

public String getBrowserVersion() {
return browserVersion;
}

public String getChromeDriver() {
return chromeDriver;
}

public String getEpoxyDefaultURL() {
return epoxyDefaultURL;
}

public String getSassyUrl() {
return sassyUrl;
}

public String getTransiteUrl() {
return transiteUrl;
}

public String getTransiteLoginUrl() {
return transiteLoginUrl;
}

public String getIeDriver() {
return ieDriver;
}

public String getWebFeatureLocation() {
return webFeatureLocation;
}

public String getWebTestLocation() {
return webTestLocation;
}

public String getEventLog() {
return eventLog;
}

public String getEpoxyBackend() {
return epoxyBackend;
}

public String getEpoxyHost() {
return epoxyHost;
}

public String getEpoxyPort() {
return epoxyPort;
}

public String getEpoxyDebug() {
return epoxyDebug;
}

public String getEpoxyImplicitWait() {
return epoxyImplicitWait;
}

public String getEpoxyTimeOut() {
return epoxyTimeOut;
}


public String getEaseLoginUrl() {
return easeLoginUrl;
}

@Override
public String toString() {
return "Ease application Default Properties [browserName=" + browserName + ", browserVersion=" + browserVersion
+ ", chromeDriver=" + chromeDriver + ", ieDriver=" + ieDriver + ", webFeatureLocation="
+ webFeatureLocation + ", webTestLocation=" + webTestLocation + ", eventLog=" + eventLog
+ ", epoxyBackend=" + epoxyBackend + ", epoxyHost=" + epoxyHost + ", epoxyPort=" + epoxyPort
+ ", epoxyDebug=" + epoxyDebug + ", epoxyImplicitWait=" + epoxyImplicitWait + ", epoxyTimeOut="
+ epoxyTimeOut + ", epoxyDefaultURL=" + epoxyDefaultURL + ", easeLoginUrl=" + easeLoginUrl + "]";
}

}

测试环境bean:

@Component
public class TestEnv implements GenericEnv {

private String envName = "test";

@Value("${profile.name}")
private String profileName;

public String getEnvName() {
return envName;
}

public void setEnvName(String envName) {
this.envName = envName;
}

public String getProfileName() {
return profileName;
}

public void setProfileName(String profileName) {
this.profileName = profileName;
}

@Override
public String toString() {
return "TestEnv [envName=" + envName + ", profileName=" + profileName
+ "]";
}

}

使用的上下文 xml:

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- scans for annotated classes in the com.company package -->
<context:component-scan base-package="com.glue.commons" />

<!-- enables annotation based configuration -->
<!-- <context:annotation-config /> -->

<beans profile="dev">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-dev files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-web-default.properties, classpath:properties/application-web-dev.properties"
ignore-unresolvable="true" />

<!-- scans for annotated classes in the com.env.dev package -->
<context:component-scan base-package="com.glue.env.dev" />
</beans>

<beans profile="test">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-test files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-web-default.properties, classpath:properties/application-web-qa2.properties"
ignore-unresolvable="true" />

<!-- scans for annotated classes in the com.env.test package -->
<context:component-scan base-package="com.glue.env.test" />
</beans>

<beans profile="prod">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-prod files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-web-default.properties, classpath:properties/application-web-prod.properties"
ignore-unresolvable="true" />

<!-- scans for annotated classes in the com.env.prod package -->
<context:component-scan base-package="com.glue.env.prod" />
</beans>
<beans profile="dev">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-dev files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-api-default.properties, classpath:properties/application-api-dev.properties"
ignore-unresolvable="true" />

<!-- scans for annotated classes in the com.env.dev package -->
<context:component-scan base-package="com.glue.env.dev" />
</beans>

<beans profile="test">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-test files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-api-default.properties, classpath:properties/application-api-qa2.properties"
ignore-unresolvable="true" />

<!-- scans for annotated classes in the com.env.test package -->
<context:component-scan base-package="com.glue.env.test" />
</beans>

<beans profile="prod">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-prod files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-api-default.properties, classpath:properties/application-api-prod.properties"
ignore-unresolvable="true" />

<!-- scans for annotated classes in the com.env.prod package -->
<context:component-scan base-package="com.glue.env.prod" />
</beans>

提前致谢,如果我犯了一些愚蠢的错误,请原谅我,但我需要你的帮助。

最佳答案

如果你使用main方法来创建它,那么spring将不知道这个类的任何信息,因此它不会 Autowiring 任何类 - 你会在所有用@注释的字段中得到空值自动连线

您的 JUnit 工作正常,因为它是使用 spring 感知的 junit 运行程序实例化的。

关于java - 使用 Spring 加载显示 NPE 的 java 属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38107474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com