Java spring 4@PropertySource相对路径
我有一个文件夹。和此文件夹中的jar应用程序。我在这个文件夹中还有一个属性文件。我的课程是:Java spring 4@PropertySource相对路径,java,spring,properties,Java,Spring,Properties,我有一个文件夹。和此文件夹中的jar应用程序。我在这个文件夹中还有一个属性文件。我的课程是: @Configuration @Import({com.blabla.MyClass.class}) @ComponentScan(basePackages = "com.blabla") **@PropertySource("file:///worker.core.properties")** @EnableTransactionManagement public class MainConfig
@Configuration
@Import({com.blabla.MyClass.class})
@ComponentScan(basePackages = "com.blabla")
**@PropertySource("file:///worker.core.properties")**
@EnableTransactionManagement
public class MainConfig {
@Autowired
private Environment env;
这个声明没有看到我的文件,但它在同一个目录中。如何指定相对路径,例如在@Import注释中?谢谢所以解决方案基于这样一个事实,即我们可以通过在清单文件中指定项来扩展类路径。所以我们需要 1) 将属性文件保留在/src/main/resources中 2) 从最后一个罐子中排除它
<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>
org.apache.maven.plugins
maven jar插件
2.4
**/*.物业
com.blabla.daemon.mainlister
解放党/
真的
形态/
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>
org.apache.maven.plugins
maven资源插件
2.3
复制资源
安装
复制资源
${basedir}/target/conf
src/main/resources
**/*.物业
5) 指定指向conf文件夹的清单文件(步骤1的一部分-参见此处)
com.blabla.daemon.mainlister
解放党/
真的
形态/
您是否尝试过@PropertySource(“file://worker/core/properties)
?谢谢,是的,这是回复。org.springframework.beans.factory.BeanDefinitionStoreException:未能解析配置类[com.blablabla.daemon.config.MainConfig];嵌套异常i s java.io.FileNotFoundException:\worker\core\properties(系统找不到指定的路径)尝试此异常@PropertySource(“classpath:worker.core.properties”)
谢谢,但只有当我的jar中有文件时,它才能工作。但我想把它放在jar文件所在的同一个文件夹之外。
<archive>
<manifest>
<mainClass>com.blabla.daemon.MainListener</mainClass>
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>