[英]Spring: Can not read system property value set by maven
我的Maven配置看起來像
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemProperties>
<spring.active.profiles>development</spring.active.profiles>
</systemProperties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
我有一個試圖設置其值的類
import javax.annotation.Nonnull;
import org.springframework.beans.factory.annotation.Value;
public class PropertyReader {
@Value("${spring.active.profiles}")
private String profile;
@Nonnull
public String getProfile() {
return profile;
}
}
我嘗試將其測試為
public class PropertyReaderTest {
@Test
public void testGetProfile() throws Exception {
assertEquals("development", new PropertyReader().getProfile());
}
}
我認為測試失敗
Failed tests: testGetProfile(com.org.myproj.external_services.ifs.PropertyReaderTest): expected:<development> but was:<null>
我在這里想念什么?
UPDATE
我看到該屬性是由Maven設置的
@Test
public void testGetProfile() throws Exception {
System.out.printf(System.getProperty("spring.active.profiles"));
assertEquals("development", new PropertyReader().getProfile());
}
我懂了
development
java.lang.AssertionError:
Expected :development
Actual :null
我什至嘗試了不同的語法
public class PropertyReader {
// @Value("#{systemProperties[spring.active.profiles]")
@Value("${spring.active.profiles")
private String profile;
@Nonnull
public String getProfile() {
return profile;
}
}
但還是看不懂
短答案
在maven-surefire-plugin配置中不建議使用systemProperties。 使用@Value("#{systemProperties['spring.active.profiles']}")
和@Value("#{systemProperties['spring.active.profiles']}")
長答案
我對其進行了測試,並且工作正常。 我的測試詳細信息在這里。
的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>net.yazilimsal</groupId>
<artifactId>sampleapp</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<spring.active.profiles>development</spring.active.profiles>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
PropertyReader.java
import org.springframework.beans.factory.annotation.Value;
public class PropertyReader {
@Value("#{systemProperties['spring.active.profiles']}")
private String profiles;
public String getProfiles() {
return profiles;
}
public void setProfiles(String profiles) {
this.profiles = profiles;
}
}
AppConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public PropertyReader propertyReader() {
return new PropertyReader();
}
}
PropertyReaderTest.java
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class PropertyReaderTest {
@Autowired
private PropertyReader propertyReader;
@Test
public void testSpringActiveProfiles() throws Exception {
String profiles = propertyReader.getProfiles();
System.out.println(profiles);
Assert.assertEquals("development", profiles);
}
}
運行命令
> mvn test
輸出:
-------------------------------------------------- -----測試-------------------------------------------- -----------運行net.yazilimsal.spring.PropertyReaderTest 2014年5月1日,下午org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO:刷新org.springframework.context.support。 GenericApplicationContext @ 2abe0e27:啟動日期[2014年5月1日星期四22:49:44 EEST]; 上下文層次結構的根2014年5月1日下午10:49:45 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO:在org.springframework.beans.factory.support.DefaultListableBeanFactory@320cf66b中預實例化單例: org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,AppConfig中,org.springframework.context.annotation。 ConfigurationClassPostProcessor.importAwareProcessor,propertyReader]; 工廠層次結構開發的根源運行測試:1,失敗:0,錯誤:0,跳過:0,經過的時間:0.793秒-在net.yazilimsal.spring.PropertyReaderTest中
結果:
測試運行:1,失敗:0,錯誤:0,跳過:0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.