【问题标题】:Java Main Class Apache MavenJava 主类 Apache Maven
【发布时间】:2023-04-04 20:21:01
【问题描述】:

我正在使用 Apache Maven 和 Spring。在 src/main/resources 文件夹中,我有一个属性文件。这些属性值可以有不同的值。

我正在使用 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/main/resources/myapp.properties 中的原始值

我想打印桌面 myapp.properties 上的值。

我做错了什么?

【问题讨论】:

  • 给你,stackoverflow.com/questions/4097614/…这应该回答你的问题
  • 我认为这不能回答我的问题。我还是不明白我做错了什么?你能举一些例子吗?
  • 让 Maven 远离儿童!它可能有毒!

标签: java spring maven jar


【解决方案1】:

目前你有

    @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

【讨论】:

  • 谢谢。 properties() 方法如何在 properties(location) 中接受?我了解这种方法,但不了解该位置的注入方式。
  • 在应用程序在 main 方法中运行后创建属性 bean 定义。实际上不是这个地方,但你明白了。我会很快为您找到更多信息的链接
猜你喜欢
  • 2022-01-07
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多