2014-09-23 54 views
0

我正在使用Apache Maven和Spring。在src/main/resources文件夾中我有一個屬性文件。這些屬性值可以有不同的值。Java主類Apache Maven

我正在使用PropertyPlaceholderConfigurer。

@Configuration 
public class ResourceConfig { 

@Bean 
public PropertyPlaceholderConfigurer properties() { 
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); 
    ppc.setIgnoreResourceNotFound(true); 
    ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile"), new ClassPathResource("myapp.properties")}); 
    return ppc; 
} 
} 

我在運行時替換這些值:

@Configuration 
public class DataSourceConfig { 

@Value("${jdbc.url}") 
private String jdbcUrlDefault; 
     // there are more @Value 
} 

這僅僅是一個樣品。我有一個主要方法:

public static void main(String[] args) { 
    // accept a properties file and override those values defined in DataSourceConfig class 
    ApplicationContext application = new AnnotationConfigApplicationContext("packaageThatContainsConfiguration"); 
    DataSourceConfig dataSourceConfig = (DataSourceConfig) application.getBean("dataSourceConfig"); 
    System.out.println(dataSourceConfig.getJdbcUrlDefault()); 
} 


<build> 
<plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <version>2.4</version> 
     <configuration> 
      <descriptorRefs> 
       <descriptorRef>jar-with-dependencies</descriptorRef> 
      </descriptorRefs> 
      <archive> 
       <manifest> 
         <mainClass>com.Main</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
    <finalName>server-app</finalName> 
</build> 

當Apache Maven構建應用程序時,屬性文件將位於類路徑中。我想用提供的屬性文件覆蓋屬性值。

我想通過被取代的主程序接受一個屬性文件,所以Spring端啓動PropertyPlaceholderConfigurer。

我有一個屬性文件,包含我想要的值。

當我在命令提示符下運行以下命令:

java -jar server-app-jar-with-dependencies.jar -D myapp.properties "C:\Users\user\Desktop\myapp.properties" 

它打印出被放置在SRC /主/資源/ myapp.properties原值

我要打印值在桌面myapp.properties上。

我在做什麼錯?

+0

在這裏你去, http://stackoverflow.com/questions/4097614/spring-configuration-with-system-property 這應該回答你的問題 – Ernie 2014-09-23 12:16:02

+0

我不認爲這個答案我的查詢。我仍然不明白我做錯了什麼?你能否提供一些例子? – user 2014-09-23 12:29:18

+0

讓Maven遠離兒童!它可能有毒! – UDPLover 2014-09-23 13:07:59

回答

0

目前你有

@Bean 
    public PropertyPlaceholderConfigurer properties() { 
     PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); 
     ppc.setIgnoreResourceNotFound(true); 
     ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile"), new ClassPathResource("myapp.properties")}); 
     return ppc; 
    } 

如果有屬性,你可以改變什麼接受一個參數說「位置」 什麼,你就可以做的是

if(location) { 
     Resource file = new FileSystemResource(location); 
     ppc.setLocations(file); 
    } else { 
     ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile"), new ClassPathResource("myapp.properties")}); 
    } 

這時如果有一個位置ARG它將使用默認的類別路徑

----將鏈接添加到相關主題----- Dealing with command line arguments and Spring

+0

謝謝。 Property()方法如何在屬性(位置)中接受?我理解這個方法,但不知道這個位置會被注入。 – user 2014-09-23 13:15:06

+0

在主要方法中運行應用程序之後,您將創建屬性bean定義..實際上並不是這個地方,但您明白了。生病迅速找到你的鏈接到更多的信息 – Ernie 2014-09-23 14:19:24