【问题标题】:spring 4 @PropertySource relative pathspring 4 @PropertySource 相对路径
【发布时间】:2023-03-23 14:42:01
【问题描述】:

我有一个文件夹。以及此文件夹中的 jar 应用程序。我也在这个文件夹中有一个属性文件。我的会议。类是:

@Configuration
@Import({com.blabla.MyClass.class})
@ComponentScan(basePackages = "com.blabla")
**@PropertySource("file:///worker.core.properties")**
@EnableTransactionManagement

public class MainConfig {
@Autowired
private Environment env;

而且这个声明看不到我的文件,但它在同一个目录中。如何指定相对路径,例如在 @Import 注释中?谢谢

【问题讨论】:

  • 你试过@PropertySource("file://worker/core/properties")吗?
  • 谢谢,是的,这就是回复。 org.springframework.beans.factory.BeanDefinitionStoreException:无法解析配置类[com.blabla.daemon.config.MainConfig];嵌套异常是 java.io.FileNotFoundException:\worker\core\properties(系统找不到指定的路径)
  • 试试这个@PropertySource("classpath:worker.core.properties")
  • 谢谢,但只有当我的 jar 中有文件时它才有效。但我想把它放在jar文件所在的同一个文件夹之外。

标签: java spring properties


【解决方案1】:

所以解决方案是基于我们可以通过在清单文件中指定项目来扩展类路径的事实。所以我们需要

1) 将属性文件留在 /src/main/resources

2) 从最终的 jar 中排除它

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <mainClass>com.blabla.daemon.MainListener</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

3) 在 jar 之外创建一个文件夹 conf

4) 使用 maven 从资源文件夹中复制属性文件

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/conf</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

5) 指定指向 conf 文件夹的清单文件(步骤 1 的一部分 - 参见此处)

 <archive>
    <manifest>
      <mainClass>com.blabla.daemon.MainListener</mainClass>
      <classpathPrefix>lib/</classpathPrefix>
         <addClasspath>true</addClasspath>
     </manifest>
     <manifestEntries>
       <Class-Path>conf/</Class-Path>
     </manifestEntries>
  </archive>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-08
    • 2011-12-15
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多