2014-05-01 46 views
0
设置的系统属性值

我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; 
    } 
} 

但仍无法读取它

回答

1

简短的回答

systemProperties在Maven的万无一失,插件配置已经过时。使用systemPropertyVariables和@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日下午10点49分44秒 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO:刷新 org.springframework.context.support。GenericApplicationContext @ 2abe0e27: 启动日期[Thu May 01 22:49:44 EEST 2014];上下文 层级的根2014年5月1日下午10点49分45秒 org.springframework.beans.factory.support.DefaultListableBeanFactory 的preInstantiateSingletons INFO:预实例单身在 org.s[email protected]320cf66b : 限定豆 [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,AppConfig中,有机springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,propertyReader]; 根的工厂层级发展试验运行:1,故障:0, 错误:0,跳过:0,所经过的时间:0.793秒 - 在 net.yazilimsal.spring.PropertyReaderTest

结果:

测试运行:1,故障:0,错误:0,跳过:0